none
uri template dosnt work in my wcf web service RRS feed

  • Question

  • Hello guys.

    i simply create a test wcf project. here is my web service:

    1- TestWebService.svc

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace WcfWebService
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "TestWebService" in code, svc and config file together.
        // NOTE: In order to launch WCF Test Client for testing this service, please select TestWebService.svc or TestWebService.svc.cs at the Solution Explorer and start debugging.
        public class TestWebService : ITestWebService
        {
            public DisCalResult[] DiscuntCalculater(string fn, string ln)
            {
                DisCalResult[] DCResult = null;
                Calculater GetDis = new Calculater();
                IPClass IP = new IPClass();
                string iP = IP.GetIPAddress();
    
                DCResult = GetDis.DisCal(fn, ln, iP);
                return DCResult;
            }
    
            public DisCalResult[] DiscuntCalculaterJson(string fn, string ln)
            {
                DisCalResult[] DCResult = null;
                Calculater GetDis = new Calculater();
                IPClass IP = new IPClass();
                string iP = IP.GetIPAddress();
    
                DCResult = GetDis.DisCal(fn, ln, iP);
                //return new JavaScriptSerializer().Serialize(DCResult);
                return DCResult;
            }
        }
    }
    

    2- TestWebService.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.ServiceModel.Web;
    
    namespace WcfWebService
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ITestWebService" in both code and config file together.
        [ServiceContract]
        public interface ITestWebService
        {
            [OperationContract]
            DisCalResult[] DiscuntCalculater(string fn, string ln);
    
            [OperationContract]
            [WebInvoke(Method="Get", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/fn/{fn}/ln/{ln}")]
            DisCalResult[] DiscuntCalculaterJson(string fn, string ln);
        }
    }
    

    and my web.config

    <?xml version="1.0"?>
    <configuration>
    
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5"/>
      </system.web>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>
    
    </configuration>
    

    when i start the project WCF test project work just fine. but when i use this url in my browser nothing shows:

    localhost:17951/TestWebService.svc/fn/foo/ln/foo

    i aslo saw the wcf has a help page. how can active it?

    is all of it about web.config file?

    Monday, June 8, 2015 6:34 AM

Answers

  • i solve it with this web.config file:

    <?xml version="1.0"?>
    <configuration>
    
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5"/>
      </system.web>    
      <system.serviceModel>
        
        <services>
          <service name="WcfWebService.TestWebService" behaviorConfiguration="ServiceBehavior">
            <endpoint binding="webHttpBinding" contract="WcfWebService.ITestWebService"
                      behaviorConfiguration="webHttp"/>
          </service>
        </services>
        
        <behaviors>
          <serviceBehaviors>
    
            <behavior name="ServiceBehavior">
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="true"/>
            </behavior>
          
          </serviceBehaviors>
          
          <endpointBehaviors>
            <behavior name="webHttp">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
          
        </behaviors>
        <protocolMapping>
            <add binding="webHttpBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>
    
    </configuration>
    

    but now i have few questions:

    1- is this possible to have rest and soap web services in a application?(it means can we config the web.config file to host both of them? )

    2- is this common to do it?(having both in a application)

    thank you.

    • Marked as answer by Bouki Tuesday, June 9, 2015 6:36 AM
    Monday, June 8, 2015 12:35 PM

All replies

  • i solve it with this web.config file:

    <?xml version="1.0"?>
    <configuration>
    
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5"/>
      </system.web>    
      <system.serviceModel>
        
        <services>
          <service name="WcfWebService.TestWebService" behaviorConfiguration="ServiceBehavior">
            <endpoint binding="webHttpBinding" contract="WcfWebService.ITestWebService"
                      behaviorConfiguration="webHttp"/>
          </service>
        </services>
        
        <behaviors>
          <serviceBehaviors>
    
            <behavior name="ServiceBehavior">
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="true"/>
            </behavior>
          
          </serviceBehaviors>
          
          <endpointBehaviors>
            <behavior name="webHttp">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
          
        </behaviors>
        <protocolMapping>
            <add binding="webHttpBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>
    
    </configuration>
    

    but now i have few questions:

    1- is this possible to have rest and soap web services in a application?(it means can we config the web.config file to host both of them? )

    2- is this common to do it?(having both in a application)

    thank you.

    • Marked as answer by Bouki Tuesday, June 9, 2015 6:36 AM
    Monday, June 8, 2015 12:35 PM
  • Hi Bouki,

    I am glad to know that you have solved your problem. :)

    1- is this possible to have rest and soap web services in a application?(it means can we config the web.config file to host both of them? )
    2- is this common to do it?(having both in a application)
    Yes, it is possible and it is common, in my mind it will make the service become more flexible. For how to have rest and soap web services in a application, please try to refer to the following articles:
    #How to enable REST and SOAP both on the same WCF Service:
    http://debugmode.net/2011/12/22/how-to-enable-rest-and-soap-both-on-the-same-wcf-service/ .
    #Exposing WCF services with SOAP and REST endpoints:
    https://jaysongo.wordpress.com/2013/01/02/exposing-wcf-services-with-soap-and-rest-endpoints/ .


    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 11, 2015 2:17 AM
    Moderator