2013-07-14 70 views
7

Trường hợp trong các mã web.config nên các khối mã sau đây đi cho một dịch vụ WCF RESTful?Định cấu hình dịch vụ nghỉ ngơi wcf trong web.config

<endpoint address="" binding="webHttpBinding"contract="Wcf_Test.IMyService"  
behaviorConfiguration="httpEndpointBehavour"> 
<identity> 
<dns value="localhost"/> 
<Identity> 
</endpoint> 

<behaviors> <serviceBehaviors> 
<behavior name="httpBehaviour"> <serviceMetadata httpGetEnabled="True"/> 
<serviceDebug includeExceptionDetailInFaults="False"/> 
</behavior></serviceBehaviors> 

<endpointBehaviors> 
<behavior name="httpEndpointBehavour"> 
<webHttp /> </behavior> 
</endpointBehaviors> 
+1

Sử dụng WCF biên tập viên SVC config. – avi

Trả lời

24

Để cấu hình một dịch vụ WCF REST, bạn cần có một vài điều trong tập tin web.config của bạn

1) Khai báo dịch vụ của bạn và điểm cuối của dịch vụ

<services> 
    <service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior"> 
    <endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService" 
       behaviorConfiguration="webHttp"/> 
    </service> 
</services> 

Tên dịch vụ sẽ là [tên dự án]. [Tên dịch vụ] Cấu hình hành vi sẽ trùng tên với hành vi bạn khai báo trong bước tiếp theo Ràng buộc phải là webHttpBinding vì bạn muốn nó là REST. Nếu bạn muốn SOAP, bạn khai báo là basicHttpBinding Hợp đồng là [tên dự án]. [Tên giao diện] Cấu hình hành vi trong điểm cuối sẽ là tên bạn khai báo ở bước tiếp theo

2) Khai báo hành vi dịch vụ (thường là mặc định)

<behavior name="ServiceBehavior" > 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="false" /> 
    </behavior> 

tên hành vi có thể được bất cứ điều gì, nhưng nó sẽ được sử dụng để phù hợp với BehaviorConfiguration bạn khai báo trong bước 1 Rời khỏi phần còn lại một mình

3) khai báo hành vi đầu cuối của bạn

<endpointBehaviors> 
    <behavior name="webHttp"> 
     <webHttp/> 
    </behavior> 
    </endpointBehaviors> 

Tên hành vi có thể là bất kỳ điều gì, nhưng tên này sẽ được sử dụng để khớp với hành viĐịnh cấu hình tại điểm cuối.

Cuối cùng, đây là những gì web.config nên trông giống như một dịch vụ REST đơn giản:

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 

    <services> 
     <service name="SparqlService.SparqlService" behaviorConfiguration="ServiceBehavior"> 
     <endpoint binding="webHttpBinding" contract="SparqlService.ISparqlService" 
        behaviorConfiguration="webHttp"/> 
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 

     <behavior name="ServiceBehavior" > 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 


     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="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="false"/> 
     </behavior> 
     </serviceBehaviors> 

     <endpointBehaviors> 
     <behavior name="webHttp"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 

    </behaviors> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 
+3

Bài đăng này hữu ích cho người mới bắt đầu. Sẽ tốt hơn nếu có lời giải thích về sự ràng buộc và ý nghĩa của chúng. Dù sao, cảm ơn. –

+0

Bất cứ điều gì để liên kết HTTP cơ bản? – AskMe

1

cho các loại còn lại sử dụng WCFservice

<configuration> 
    <system.serviceModel> 
     <services> 
      <service> 
    <-- 
     "place the first code snippet here " 
     it will contain the endpoint details 
     for WCFrestfulServices it will have 'A' ,'B' and 'C' 
     that is address, binding and contract 
    --> 
      </service> 
     </services> 
     <behaviors> 
     <servicebehaviours> 
    <-- 
     "place the second code snippet" 
     the name of the behavior should be the same to that of the 
     behavior configuration attribute value of service tag 
    --> 
     </servicebehaviours> 
     <endpointBehaviors> 
    <-- 
     "place your third code snippet" 
     the name of the behavior should be the same to that of the 
     behavior configuration attribute value of endpoint tag 
    --> 
     </endpointBehaviors> 
     </behaviors> 
    </system.serviceModel> 
</configuration> 
Các vấn đề liên quan