convert a WCF Service, to a RESTful application?

前端 未结 4 2044
北荒
北荒 2021-01-02 04:45

Hey im not getting anywhere with turning wcf into a restful service. So I was wondering if some one can take the basic code when you start a WCF Service application here:

相关标签:
4条回答
  • 2021-01-02 05:04

    As you had mentioned, this is a new project, so maybe a redirect might help make the process a little more simple?

    Here is an MSDN article about what you are asking.

    However, I especially suggest looking at ServiceStack to create a RESTful service, as it makes the process extremely easy. WCF definitely does not provide an easy means to accomplishing this. They overcomplicate it in this case IMO.

    That would be where I would go with this instead, if this is indeed the beginnings of a project

    A more direct answer comes from This article that is a bit older, but can probably help both understand REST, and how to implement it in WCF. And is to specify your GET/POST/PUT/DELETE in a Web[Type]Attribute

    [WebGet(UriTemplate = @"Data?value={value}")]
    [OperationContract]
    string GetData(int value);
    

    Also, you will need to do this in your application's .config (Again for the older, MSDN article by Skonnard)

    <configuration>
       <system.serviceModel>
         <services>
            <service name="Service1">
                <endpoint binding="webHttpBinding" contract="Service1"
                          behaviorConfiguration="webHttp"/>
            </service>
         </services>
         <behaviors>
            <endpointBehaviors>
                <behavior name="webHttp">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
         </behaviors>
      </system.serviceModel>
    <configuration>
    

    Which would translate into your config as:

    <system.serviceModel>
    <services>
      <service name="WcfService1.Service1"
            behaviorConfiguration="WcfService1.Service1Behavior">
        <!-- Service Endpoints -->
        <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1"
            behaviorConfiguration="webHttp">
          <!-- 
              Upon deployment, the following identity element should be removed 
              or replaced to reflect the identity under which the deployed service runs.  
              If removed, WCF will infer an appropriate identity automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService1.Service1Behavior">
          <!-- 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>
    

    Then, you will probably need to add an address attribute to the endpoint so it knows where to look.

    0 讨论(0)
  • 2021-01-02 05:10

    When configuring a simple WCF REST service, in the past I've had an error similar to what your having. In the article mentioned by Justin:
    A Guide to Designing and Building RESTful Web Services with WCF 3.5
    (search for - Defining the HTTP Interface: [WebGet])

    You'll notice that the Get methods all take strings.

    The error you have is common when you try and convert one of the sample WCF projects to a RESTful one. To fix, simply change the signature of the method and the interface to accept a string, as opposed to an int, this is what inner exception is complaining about:

    Operation 'GetData' in contract 'IService1' has a path variable named 'value' which does not have type 'string'. Variables for UriTemplate path segments must have type 'string'

    original:

    public string GetData(int value)
    

    modified:

    public string GetData(string value)
    

    Here's a simple .config section from a sample project I have, that I know works:

    <system.serviceModel>
        <behaviors>
          <endpointBehaviors>
            <behavior name="Service1Behavior">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <services>
          <service name="Wcf.Service1">
            <endpoint address=""
                    behaviorConfiguration="Service1Behavior"
                    binding="webHttpBinding"
                    contract="Wcf.IService1"/>
          </service>
        </services>
      </system.serviceModel>
    
    0 讨论(0)
  • 2021-01-02 05:17

    This is example project from codeproject.com. There is need to make

    [WebGet(UriTemplate = "?id={id}")]
    

    instead of

    [WebGet(UriTemplate = "{id}")]
    
    0 讨论(0)
  • 2021-01-02 05:19

    Use WebGet attribute on the operations you wish to be available as a RESTful service.

    Use webHttpBinding.

    Remember to add to your behaviors in config.

    Should be enough to get it started.

    EDIT:

    Add a new binding in under your service:

    <service><endpoint binding="webHttpBinding"... behavior="myBehavior"/> 
    

    etc - then further to that add

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

    as an endpointbehavior.

    [WebGet] attribute - look into the various options to you to further develop it.

    0 讨论(0)
提交回复
热议问题