創建Restful WCF

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>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章