gpt4 book ai didi

c# - 从 jquery/javascript 调用 Windows 服务 wcf 服务

转载 作者:太空宇宙 更新时间:2023-11-03 16:20:58 33 4
gpt4 key购买 nike

我在 Windows 服务中托管了一个 WCF(从 MS 网站示例中选取),我可以使用 SOAP UI 访问和调用方法。但是,当我尝试使用 jquery 从 Web 应用程序调用相同的方法时,我不断收到未知错误,并且来自 json 的状态代码为 12152。

下面是服务的 app.config。

<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<!-- This section is optional with the new configuration model
introduced in .NET Framework 4. -->
<service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/ServiceModelSamples/service"/>
<!--<add baseAddress="net.tcp://localhost:8081/ServiceModelSamples/service"/>-->
</baseAddresses>
</host>
<!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service -->
<endpoint address="basic" binding="basicHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/>
<endpoint address="ws" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/>
<endpoint behaviorConfiguration="web" address="wb" binding="webHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/>
<!--<endpoint address="tcp" binding="netTcpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"/>-->
<!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup> </configuration>

下面是javascript代码

function CallService() {
debugger;
$.support.cors = true;
$.ajax({

type: 'POST',
url: 'http://localhost:8080/ServiceModelSamples/service/wb/ShowMessage/',
data: '{}',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
processdata: false,
error: function (xhr) { ServiceFailed(xhr); }
});
}

下面是合约代码

[ServiceContract(Namespace="test")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
string ShowMessage();
}

关于如何从 js 代码调用服务的任何指示都会有所帮助。

谢谢

最佳答案

看来我必须手动构建 SOAP 信封才能进行此类调用。或者另一个选择似乎是,使服务成为 RESTful 服务。

关于c# - 从 jquery/javascript 调用 Windows 服务 wcf 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13891755/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com