wcf开始使用wcf


备注

本节概述了wcf是什么,以及开发人员可能想要使用它的原因。

它还应该提到wcf中的任何大型主题,并链接到相关主题。由于wcf的文档是新的,您可能需要创建这些相关主题的初始版本。

简单的WCF服务

WCF服务的最低要求是一个具有一个OperationContract的ServiceContract。

服务合约:

[ServiceContract]
public interface IDemoService
{
    [OperationContract]
    CompositeType SampleMethod();
} 
 

服务合同实施:

public class DemoService : IDemoService
{
    public CompositeType SampleMethod()
    {
        return new CompositeType { Value = "foo", Quantity = 3 };
    }
}
 

配置文件:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>
 

DTO:

[DataContract]
public class CompositeType
{
    [DataMember]
    public string Value { get; set; }

    [DataMember]
    public int Quantity { get; set; }
}
 

WCF Restful Service Demo

SVC

 public class WCFRestfulService : IWCFRestfulService 
    {
       public string GetServiceName(int Id)
        {
            return "This is a WCF Restful Service";
        }
    }
 

接口

[ServiceContract(Name = "WCRestfulService ")]
public interface IWCFRestfulService 
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetServiceName?Id={Id}")]
    string GetServiceName(int Id);
}
 

svc Markup(右键单击svc文件并单击查看MarkUp)

<%@ ServiceHost Language="C#" Debug="true" Service="NamespaceName.WCFRestfulService" CodeBehind="WCFRestfulService.svc.cs" %>
 

Web配置

<services>
      <service name="NamespaceName.WCFRestfulService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="NamespaceName.IWCFRestfulService" behaviorConfiguration="web"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
 

现在只需在端口中运行服务或主机即可。并使用“ http:// hostname / WCFRestfulService / GetServiceName?Id = 1 ”访问服务