创建Restful WCF

来源:互联网 发布:手机怎么发布淘宝宝贝 编辑:程序博客网 时间:2024/05/21 09:27

1. 创建Service类,如下:

    [ServiceContract]    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]    public class CommonService    {        [WebGet(UriTemplate = "Common/ServerDateTime/Get")]        public DateTime GetServerDateTime()        {            return DateTime.Now;        }        [WebInvoke(UriTemplate = "Common/UserName/Get", Method = "POST")]        public string GetUserName(string userName)        {            return "My name is:" + userName;        }


2. 创建SVC文件,起名:CommonService.svc,文件内容如下:

<%@ ServiceHost Language="C#" Debug="true" Service="LongCentral.Service.Common.CommonService" %>

其中Service指向第一步创建的Class。

3. 在WebConfig中添加配置文件,如下:

<?xml version="1.0"?><configuration><system.serviceModel><behaviors><serviceBehaviors><behavior name="Framework_WebHost_ServiceBehavior"><serviceMetadata httpGetEnabled="true"/><serviceThrottling maxConcurrentCalls="1000" maxConcurrentSessions="1000" maxConcurrentInstances="1000"/><serviceDebug includeExceptionDetailInFaults="true"/><dataContractSerializer maxItemsInObjectGraph="90000000"/></behavior></serviceBehaviors><endpointBehaviors><behavior name="HelpBehavior"><webHttp helpEnabled="true" faultExceptionEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/></behavior></endpointBehaviors></behaviors><bindings><webHttpBinding><binding name="Framework_WebHost_WebHttpBinding" allowCookies="false" bypassProxyOnLocal="true" maxBufferPoolSize="65536000" maxReceivedMessageSize="65536000" useDefaultWebProxy="false" closeTimeout="00:50:00" openTimeout="00:50:00" sendTimeout="00:50:00" receiveTimeout="08:00:00"><readerQuotas maxDepth="100" maxStringContentLength="9999990" maxArrayLength="9999990"/><security><transport proxyCredentialType="None"/></security></binding></webHttpBinding></bindings><standardEndpoints><webHttpEndpoint><standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" crossDomainScriptAccessEnabled="true"/></webHttpEndpoint></standardEndpoints><services><service name="LongCentral.Service.Common.CommonService" behaviorConfiguration="Framework_WebHost_ServiceBehavior"><endpoint binding="webHttpBinding" bindingConfiguration="Framework_WebHost_WebHttpBinding" behaviorConfiguration="HelpBehavior"   contract="LongCentral.Service.Common.CommonService"/></service></services></system.serviceModel></configuration>




0 0
原创粉丝点击