【发布时间】:2015-10-17 05:03:27
【问题描述】:
我正在尝试使用 JSON 和使用 ASP .NET 中的客户端创建 WCF Web 服务 我的 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 未更新客户端 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>
从客户端调用服务
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 服务读取时,出现异常
没有端点监听 http://localhost/WCFService/Service1.svc/GetData 可以接受 信息。这通常是由不正确的地址或 SOAP 操作引起的。 有关详细信息,请参阅 InnerException(如果存在)。
内部异常:远程服务器返回错误:(404) Not 找到了。”}
我怀疑这是我的 web.config 文件中的配置问题,但不确定是什么导致了这个问题。
谢谢, 阿肖克
【问题讨论】:
-
如果您将浏览器指向localhost/WCFService/Service1.svc,会发生什么?你得到漂亮的 WCF 状态页面了吗?
-
是的,我可以在 JSON 中看到 localhost/WCFService/Service1.svc/data/ashok 的响应。
-
如果您的问题有答案,如果您认为正确,请标记答案。
标签: c# asp.net json web-services wcf