gpt4 book ai didi

c# - WCF Web 服务客户端没有端点监听

转载 作者:行者123 更新时间:2023-11-30 21:56:15 25 4
gpt4 key购买 nike

我正在尝试使用 JSON 创建一个 WCF Web 服务,并在 ASP .NET 中与客户端一起使用我的 WCF Web 服务器已启动并运行在 IIS 上托管,我已检查浏览器,获取 JSON 响应。

这是服务器 web.config 文件:

<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<!--<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>-->
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfServiceApp.Service1">
<endpoint address="../Service1.svc" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/>
</service>
</services>

<behaviors>
<serviceBehaviors>
<behavior >
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/>
</customHeaders>
</httpProtocol>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>

之后,我在 ASP .NET 中创建了一个 Web 应用程序客户端来使用 WCF Web 服务。我在客户端添加了 WCF web 服务引用,但是

Visual Studio 2012 not updating client web.config

.我在 stackoverflow 中发现了一些东西对于我的客户 web.config

客户端 web.config

<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<!--<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>-->
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webby">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost/WCFService/Service1.svc" name="Service1" binding="webHttpBinding"
contract="ServiceReference1.IService1" behaviorConfiguration="webby"/>
</client>
</system.serviceModel>
</configuration>

calling Service from Client

protected void Button1_Click(object sender, EventArgs e)
{
string result;
string input = tbInput.Text;
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
try
{
result = client.GetData(input);
lbResult.Text = result;
client.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

}

但是当我尝试从 Web 服务读取时,出现异常

There was no endpoint listening at http://localhost/WCFService/Service1.svc/GetData that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

Inner Exception: The remote server returned an error: (404) Not Found."}

我怀疑它是我的 web.config 文件中的配置问题,但不确定是什么导致了这个问题。

谢谢,阿肖克

最佳答案

您正在为您的服务使用 WebHttpBinding。所以基本上这是一个 REST 服务。所以你不能添加 Service Reference 因为 REST 服务不公开任何元数据,你需要通过 HTTP 动词查询资源,比如 GET POST 左右。

试一试:

var req = (HttpWebRequest)WebRequest.Create("your endpoint");
var data_to_send = Encoding.ASCII.GetBytes("some data");
using (var _temp = req.GetRequestStream())
{
_temp.Write(data_to_send, 0, data_to_send.Length);
}

var res = req.GetResponse();

或者你可以选择:

var req = new HttpClient().PostAsync("your url", new StringContent(JsonConvert.SerializeObject("your params")));

同时在您的 endpointBehavior 中启用一些属性以捕获更具体的异常详细信息。

关于c# - WCF Web 服务客户端没有端点监听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31645843/

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