为WCF ServiceContract设置默认的RequestFormat,可以通过以下几个步骤来实现:
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebGet(RequestFormat = WebMessageFormat.Json)]
string GetData();
}
public class MyService : IMyService
{
public string GetData()
{
return "Hello, WCF!";
}
}
<services>
<service name="MyService">
<endpoint address="" binding="webHttpBinding" contract="IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
var factory = new WebChannelFactory<IMyService>(new Uri("http://localhost/MyService.svc"));
var service = factory.CreateChannel();
var result = service.GetData();
Console.WriteLine(result);
这样,就可以为WCF ServiceContract设置默认的RequestFormat了。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云