How can I use a WCF Service?

后端 未结 1 913
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 14:09

When I create a new WCF service, I can create a new method like this one:

 [OperationContract]
 [WebInvoke(Method = \"GET\", UriTemplate = \"TryThis\", Resp         


        
相关标签:
1条回答
  • 2020-11-29 14:57

    It seems that you host the WCF service in IIS, and you want to publish a Restful-style service, you could refer to the following code.
    Server:IService.cs

    namespace WcfService5
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            [WebGet]
            string GetData(int value);
        }
    }
    

    Server:Service.svc.cs

    namespace WcfService5
    {
        public class Service1 : IService1
        {
            public string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
        }
    }
    

    Web.config.

    <system.serviceModel>
        <services>
          <service name="WcfService5.Service1">
            <endpoint address="" binding="webHttpBinding" contract="WcfService5.IService1" behaviorConfiguration="MyRest"></endpoint>
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="MyRest">
              <webHttp />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    Result.

    Feel free to let me know if there is anything I can help with.

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