我是WCF的新人。我已成功在VS 2015中设置了一个工作项目。在此,我有两个Services Contracts
,每个都有一个Operation Contract
。对于我需要的复合类,我也有一个DataContract
。我已将wcf
解决方案设置为IIS
,端口8000上的网站。
我的真正问题是:如果对应于这两个合同的两个端点都在basicHttpBinding
中,当我点击F5时它们都出现在WCF客户端中,它们都可以被调用并给出预期的结果,并且它们两者都可以被测试控制台解决方案使用。
但是,只要我将第二个切换到webHttpBinding
并添加webhttp
behaviorConfiguration(以便能够使用json数据),此端点就会停止出现在WCF客户端中,当然不能再调用,我收到一个未找到的"端点"从我的测试解决方案。在这种情况下,我使用HttpWebRequest
尝试访问端点;它在错误的html消息中,我可以读取端点未找到。
也许我在这里遗漏了一些明显的东西。
以下是代码:
Service.cs
namespace WcfService2
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DbQuery : IDbQuery, IDbQueryJason
{
public string GetDescriptions(string ParametersString)
{
return "received " + ParametersString;
}
public string GetDescriptionsInJson(string ParametersString)
{
return "received2 " + ParametersString;
}
}
}
Iservice.cs:
namespace WcfService2
{
[ServiceContract]
public interface IDbQuery
{
[OperationContract]
[WebInvoke(UriTemplate = "/getdescriptions",
BodyStyle = WebMessageBodyStyle.Wrapped)]
List<Description> GetDescriptions(string Parameters);
}
[ServiceContract]
public interface IDbQueryJason
{
[OperationContract]
[WebInvoke(UriTemplate = "/getdescriptionsinjson",
BodyStyle = WebMessageBodyStyle.Wrapped)]
string GetDescriptionsInJson(string Parameters);
}
[DataContract]
public class Description
{
[DataMember]
public string IDInfo;
[DataMember]
public string CatalogInfo;
[DataMember]
public string TitleInfo;
[DataMember]
public string AuthoringInfo;
[DataMember]
public string FormatInfo;
[DataMember]
public string DateInfo;
}
}
web.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Mg">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="rfb">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<!--<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true"></binding>
</webHttpBinding>
</bindings>-->
<services>
<service name="WcfService2.DbQuery" behaviorConfiguration="Mg">
<endpoint name="getdescriptions"
address ="getdescriptions"
binding ="basicHttpBinding"
contract ="WcfService2.IDbQuery"/>
<endpoint name="getdescriptionsinjson" behaviorConfiguration="rfb"
address ="getdescriptionsinjson"
binding ="webHttpBinding"
contract ="WcfService2.IDbQueryJason"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
</service>
</services>
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
<serviceActivations>
<add factory="System.ServiceModel.Activation.WebServiceHostFactory"
relativeAddress="./Service.svc"
service="WcfService2.DbQuery"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
<handlers>
<add name="svc" path="*.svc" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" resourceType="File" preCondition="integratedMode" />
</handlers>
</system.webServer>
</configuration>
测试解决方案摘要:
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://localhost:8000/Service.svc/getdescriptionsinjson");
req.Method = "POST";