它是默认情况下在Visual Studio 2013中创建的代码。 在项目属性中,我设置为使用本地IIS。 WCF测试客户端成功测试它。 但是,如果我访问页面
http://localhost/WcfService1/Service1.svc/GetTime
在浏览器中,我看到空的浏览器屏幕和Fiddler show" 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();
}
}
Service1.svc.cs
using System;
namespace WcfService1
{
public class Service1 : IService1
{
public string GetTime()
{
return DateTime.Now.ToShortTimeString();
}
}
}
的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>
答案 0 :(得分:9)
感谢sakir和Tim,你的评论让我在适当的地方寻找答案。是的,这是SOAP服务,因此我需要重新配置它以便通过Web http访问。
注意:我们可以在Visual Studio中获得类似的结果,如果我们将通过添加空项目“WCF服务(启用Ajax)”模板(我在VS2013中很难找到它,但它在在VS2012中作为一个单独的项目排名第一)。这将为我们修改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();
}
}
的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>