如何使用WCF服务?
当我创建一个新的WCF服务时,我可以创建一个像这样的新方法:如何使用WCF服务?,wcf,Wcf,当我创建一个新的WCF服务时,我可以创建一个像这样的新方法: [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "TryThis", ResponseFormat = WebMessageFormat.Json)] string TryThis(); 返回简单字符串的函数 我可以将它与测试客户机WCF一起使用,这很有效。但当我想通过Chrome、postman或Android应用程序访问我的服务时,我不能 我尝试了以
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "TryThis", ResponseFormat = WebMessageFormat.Json)]
string TryThis();
返回简单字符串的函数
我可以将它与测试客户机WCF一起使用,这很有效。但当我想通过Chrome、postman或Android应用程序访问我的服务时,我不能
我尝试了以下网址:
http://localhost:54792/Service1.svc/TryThis
http://tempuri.org/IService1/TryThis
当我使用“”时,我有主页,所以这是可以的。但我无法使用我的任何服务方法
这个错误是400错误。但是我没有任何消息指出这个错误在哪里。我不知道这是否是我的IIS服务器的配置,这是否是我的服务。我完全被封锁了
这是我的web.config
:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.7.1" />
<httpRuntime targetFramework="4.7.1"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
“但这对我没有帮助。似乎您在IIS中托管WCF服务,并且您想要发布Restful样式的服务,您可以参考以下代码。
服务器:IService.cs
namespace WcfService5
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet]
string GetData(int value);
}
}
namespace WcfService5
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
服务器:Service.svc.cs
namespace WcfService5
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet]
string GetData(int value);
}
}
namespace WcfService5
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
Web.config.
<system.serviceModel>
<services>
<service name="WcfService5.Service1">
<endpoint address="" binding="webHttpBinding" contract="WcfService5.IService1" behaviorConfiguration="MyRest"></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MyRest">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
如果有什么我可以帮忙的,请随时告诉我。似乎您在IIS中托管WCF服务,并且您想发布Restful样式的服务,您可以参考以下代码。
服务器:IService.cs
namespace WcfService5
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet]
string GetData(int value);
}
}
namespace WcfService5
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
服务器:Service.svc.cs
namespace WcfService5
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet]
string GetData(int value);
}
}
namespace WcfService5
{
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
Web.config.
<system.serviceModel>
<services>
<service name="WcfService5.Service1">
<endpoint address="" binding="webHttpBinding" contract="WcfService5.IService1" behaviorConfiguration="MyRest"></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MyRest">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
如果有什么我可以帮忙的,请随时告诉我。您没有向我们展示web.config
中最重要的部分-服务绑定等的配置位置。默认情况下,大多数WCF绑定使用SOAP在客户端和服务器之间通信-典型的web浏览器没有内置SOAP客户端-您需要使用SoapUI或类似工具来创建和处理SOAP请求。您可以在浏览器中测试的唯一绑定是使用REST在客户端和服务器之间通信的WebHttpBinding
。这就是我的web.config
,我有一个web.debug.config
和一个web.release.config
,但没有更多。这是默认应用程序。我没有做任何更改。如果可能的话,我更喜欢使用REST与WebHttpBinding
。感谢您的编辑。我的英语不是很好。请参阅其他SO问题(及其答案!):非常感谢!非常感谢!!您没有向我们展示web.config
中最重要的部分-其中配置了服务绑定等。默认情况下,大多数WCF绑定使用SOAP在客户端和服务器之间进行通信-典型的web浏览器没有内置SOAP客户端-您需要使用SoapUI或simil之类的工具ar来创建和处理SOAP请求。您可以在浏览器中测试的唯一绑定是使用REST在客户端和服务器之间进行通信的WebHttpBinding
。这是我的web.config
,我有一个web.debug.config
和一个web.release.config
,但仅此而已。这是默认应用程序。我没有做任何更改。如果可能的话,我更喜欢使用REST和WebHttpBinding
。谢谢您的编辑。我的英语不是很好。请看另一个SO问题(及其答案!):非常感谢!非常感谢!但是@marc_s已经回答了我:),我刚刚在web.config
中添加了
和
,效果很好。所以你的解决方案是一样的!!但是现在我有另一个问题,当我使用浏览器访问我的请求时:http://localhost:54792/Service1.svc/TryThis
可以,但如果我用127.0.0.1或我的计算机IP替换“localhost”,则会出现另一个400错误。很抱歉响应太晚。在这种情况下,我建议您修改IIS站点绑定配置(IIS>>连接>>您的网站>>绑定),将IP地址更改为所有未分配。感谢您的回答,我使用IIS Express用于此应用程序,我发现了此问题,但在此页面上找不到解决方案(可能是因为答案的日期为2010年)我试图添加
行,但我在applicationhost.config
中没有关于我的项目的部分。如果我找不到解决方案,我会问另一个问题。我找到了解决方案!当我添加这行
时,我添加了错误的applicationhost.config
。因此,现在它可以正常工作了。Chrome、postman和其他计算机可以使用我的服务!!只是我的android应用程序仍然无法使用,但我很快就会找到答案!祝您愉快!!谢谢您,但是@marc_s已经回答了我:),我刚刚在web.config
中添加了
和
,这很有效。所以您的解决方案是一样的!!但是现在我有另一个问题,当我使用我的浏览器像这样访问我的请求:http://localhost:54792/Service1.svc/TryThis
这是可行的,但如果我用127.0.0.1或我的计算机IP替换“localhost”,则会出现另一个400错误。很抱歉响应太晚。在这种情况下,我建议您修改IIS站点绑定配置(IIS>>连接>>您的网站>>绑定),将IP地址更改为所有未分配。感谢您的回答,我使用IIS Express用于此应用程序,我发现了此问题,但在此页面上找不到解决方案(可能是因为答案的日期为2010年)我试图添加
行,但我在applicationhost.config
中没有关于我的项目的部分。如果我找不到解决方案,我会问另一个问题。我找到了解决方案!当我添加这行
时,我添加了错误的applicationhost.config
。因此,现在它可以正常工作了。Chrome、postman和其他计算机可以使用我的服务!!只是我的android应用程序仍然不能,但我很快就会找到答案