它是默认情况下在Visual Studio 2013中创建的代码.在项目属性中,我设置为使用本地IIS.WCF测试客户端成功测试它.但是,如果我访问页面
http://localhost/WcfService1/Service1.svc/GetTime
Run Code Online (Sandbox Code Playgroud)
在浏览器中,我看到空的浏览器屏幕和Fiddler显示"HTTP/1.1 400 Bad Request".我明白我需要修改web.config和接口中方法的属性,但不知道如何.你可以帮帮我吗?谢谢.
IService1.cs
using System.Runtime.Serialization;
using System.ServiceModel;
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetTime();
}
}
Run Code Online (Sandbox Code Playgroud)
Service1.svc.cs
using System;
namespace WcfService1
{
public class Service1 : IService1
{
public string GetTime()
{
return DateTime.Now.ToShortTimeString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
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="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
谢谢sakir和Tim,你的评论允许我在适当的地方寻找答案.是的,这是SOAP服务,所以我需要重新配置它才能通过web http访问.
注意:我们可以在Visual Studio中获得类似的结果,如果我们将通过添加空项目"WCF服务(启用Ajax)"模板(我在VS2013中很难找到它,但它在VS2012中排在首位)来创建WCF Web服务作为一个单独的项目).这将为我们修改web.config,并给出工作代码示例(但不是基于接口).

修改过的文件和重新配置的web.config如下:
IService1.cs
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet]
string GetTime();
}
}
Run Code Online (Sandbox Code Playgroud)
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.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
**<!--added behavior-->
<endpointBehaviors>
<behavior name="WcfService1.Service1AspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>**
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
**<!--added service behaviour configuration-->
<services>
<service name="WcfService1.Service1">
<endpoint address="" behaviorConfiguration="WcfService1.Service1AspNetAjaxBehavior"
binding="webHttpBinding" contract="WcfService1.IService1" />
</service>
</services>**
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18426 次 |
| 最近记录: |