1
我创建了一个简单的WCF服务应用程序,并尝试更改WCF代码,它也将能够从客户端浏览器中调用,但它只能成功从WCF运行客户。无法从浏览器运行WCF方法
从浏览器运行,不调用WCF服务(但在浏览器中没有错误消息)。
代码:
合同:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet]
string Test();
[OperationContract]
[WebGet]
string GetData(int value);
}
实现:
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public string Test()
{
return "test success";
}
}
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"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
项目网址:
http://localhost:1507/
浏览器URL行 - 测试方法:
http://localhost:1507/Service1.svc/Test
感谢。
你是什么意思“从浏览器运行”?运行什么? 'Test'是一个操作,而不是一个URL。你不能用GET调用它。这不是WCF的限制,这正是(SOAP)Web服务的工作原理。也许您是否将Web服务与REST API混淆? –
因为WCF服务从本地主机运行,我从本地浏览器运行“http:// localhost:1507/Service1.svc/Test”行。 –
我将绑定更改为binding =“webHttpBinding”,它将使用REST API运行。 –