Search code examples
c#web-serviceswcfservicecontract

WCF without service contract interface is possible


i was searching google to know that how we can easily convert asmx service to wcf and there i saw people write code like

this is my old asmx service

[WebService(Namespace="http://kennyw.com/sampleservices/")]
public class MyService : System.Web.Services.WebService
 { 
[WebMethod]
public string Hello(string name)
  { 
   return string.Format(“Hello {0}.”, name);

  }

}

convert to wcf

[ServiceContract(Namespace="http://kennyw.com/WCFservices/")]
[WebService(Namespace="http://kennyw.com/sampleservices/")]
public class MyService : System.Web.Services.WebService
 { 
[WebMethod]
[OperationContract]
public string Hello(string name)
   { 
    return string.Format(“Hello {0}.”, name);

   }

}

i saw when converting then people is not using service contract interface. how it will work because i know that when we develop wcf service then i have to write one service contract interface for single service. so looking for discussion that how possible to develop wcf service without service contract interface. thanks


Solution

  • The above will not work in case you do not specify a correct contract naming in your web.config.

    In case your class without an interface is in the following namespace:

    namespace Foo
    {
        [ServiceContract(Namespace="http://kennyw.com/WCFservices/")]
        [WebService(Namespace="http://kennyw.com/sampleservices/")]
        public class MyService : System.Web.Services.WebService
        { 
            [WebMethod]
            [OperationContract]
            public string Hello(string name)
            { 
                return string.Format(“Hello {0}.”, name);
    
            }
        }
    }
    

    In this case the web config service configuration should look like

    <services>
        <service name="YourServiceName">
            <endpoint address="" behaviorConfiguration="httpBehavior" binding="webHttpBinding" contract="Foo.MyService"/>
        </service>
    </services>