[英]WCF Testing Client giving right results but not when executed in Chrome
我正在尝试构建我的第一个WCF服务。 我现在有以下行为。
http://localhost:12345/Service1.svc
,我得到一个页面。 svcutil.exe http://localhost:12345/Service1.svc?wsdl
会给我一个XML。 但是,当我输入http:// localhost:12345 / Service1.svc / test / 13时 ,我只会得到一个空响应。 除了<body>
和<pre>
什么都没有。 我该怎么办?我该如何解决? (请记住,我是新手。)一旦按照我想要的方式运行行为(以便在浏览器中看到正确的结果),我将以XML生成REST或JSON数据格式(如果有任何重要性)。
从讨论中我得到了这一点。
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "/test/{indata}",
ResponseFormat = WebMessageFormat.Xml)]
String Ping(String indata);
}
}
从这个问题可以看出,我的实现如下。
namespace WcfService1
{
public class Service1 : IService1
{
public string Ping(String indata)
{
return "Pong " + indata;
}
}
}
建议的web.config无法正常工作,因此我尝试结合本文的讨论使用本文中的指针发布元数据(无论是哪种形式)。 我的配置文件看起来与后一个链接中的配置文件差不多(除了我已删除诊断部分)。
我相信WCF测试客户端在SOAP上运行。 它测试的是您正在提供某些东西的事实,而不是您正在提供想要得到的东西。
根据我的经验,您得到的空的身体不过是一条错误消息。 但是,在某些情况下,例如跨域调用(不确定名称是否正确,也不是可能出现的问题的完整列表),当您使用JavaScript中的XDomainRequest对象(与通常的XmlHttpRequest相对)时,响应为空是错误消息的结果。
您是否尝试检查状态码? 是200 OK还是更大的东西?
我相信您在Social MSDN上也提出了类似的问题,并且在如何编写代码方面有些困惑。 让我回顾一下下面的要点。
Web.config文件
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
...
</services>
<behaviors>
</behaviors>
</system.serviceModel>
</configuration>
服务 -描述服务性质的标签内容
<service name="DemoRest.RestService"
behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding"
contract="DemoRest.IRestService"
behaviorConfiguration="web"></endpoint>
</service>
behaviors-标记的内容,描述服务的行为和端点
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
现在,可以使用以下URL检索响应。
localhost/Service1.svc/inputData
您可以尝试以下操作:
使用ServiceBehaviour属性标记服务实现
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] public class Service1 : IService1
在web.config内,添加/修改以下保留现有数据:
<services> <service name="WcfService1.Service1"> <endpoint binding="webHttpBinding" contract="WcfService1.IService1" /> </service> </services> <behaviors> <endpointBehaviors> <behavior> <webHttp /> </behavior> </endpointBehaviors> ... </behaviors>
这些步骤使其正常工作。
为了使REST使用dot net framework 4简化的配置工作 ,您的web.config需要包含以下内容:
<system.serviceModel>
<!-- 1) Specify webHttp as an endpoint behavior -->
<behaviors>
<endpointBehaviors>
<behavior >
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<!-- 2) Setup a protocol mapping to allow the service to be accessed via webHttp-->
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
</system.serviceModel>
要获得浏览器中没有所有xml的输出,请添加以下内容:
<!-- Configure the webHttp standard endpoint -->
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
要允许访问服务元数据(需要轻松创建代理),请将其添加到behaviors元素:
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
我个人更喜欢旧式配置,在该配置中可以显式配置端点,如下所示:
<system.serviceModel>
<services>
<service name="WcfService1.Service1">
<endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="webHttpBehavior"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp automaticFormatSelectionEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
最后,您可以使用WCF服务配置编辑器使用gui进行配置。 您可以在Visual Studio的工具菜单上找到它。 打开后,用它打开项目的web.config并开始编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.