Gangadhar
Gangadhar

Reputation: 1749

unable to add refrence of restful wcf service in windows service

When i tried to add the reference of restful wcf service to windows service. I am getting "The type or namespace name 'RestfulService' could not be found (are you missing a using directive or an assembly reference?)" error.

MY Interface Is

[ServiceContract(Name = "RJContract",
     Namespace = "RestfulService",
     SessionMode = SessionMode.Allowed)]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "/rjdata/{name}")]
        string RJData(string name);
    }

App.Config

<system.serviceModel>
    <services>
      <service name="RestfulService.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8732/RestfulService/Service1/" />
          </baseAddresses>
        </host>
        <endpoint  binding="webHttpBinding" contract="RestfulService.IService1" bindingConfiguration="RESTBindingConfiguration"
                   behaviorConfiguration="RESTEndpointBehavior"/>

      </service>
    </services>



    <bindings>
      <webHttpBinding>
        <binding name="RESTBindingConfiguration">
          <security mode="None" />
        </binding>
      </webHttpBinding>

      <netTcpBinding>
        <binding name="DefaultBinding">
          <security mode="None"/>
        </binding>
      </netTcpBinding>

    </bindings>



    <behaviors>
      <endpointBehaviors>
        <behavior name="RESTEndpointBehavior">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true"/>
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
  </system.serviceModel>

But i am able to add the reference with the following.

 [ServiceContract(Name = "RJContract",
         Namespace = "RestfulService",
         SessionMode = SessionMode.Allowed)]
        public interface IService1
        {
            [OperationContract]
            string RJData(string name);
        }

In windows Hosting

public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
        ServiceHost sHost;
        protected override void OnStart(string[] args)
        {
            try
            {
                sHost = new ServiceHost(typeof(RestfulService.Service1));
                sHost.Open();
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(ex.Message);
            }
        }

        protected override void OnStop()
        {
        }
    }

where RestfulService is my reference to the wcf service

Upvotes: 1

Views: 2052

Answers (3)

Dienekes
Dienekes

Reputation: 1548

You are confusing yourself by overlapping standards of SOAP and REST.
RESTful style of services do not follow SOAP standards. The Add Reference feature in VS downloads the metadata (including WSDL) of a SOAP based service to know about its contracts/bindings/etc. However, in case of a REST based service these standards/mechanisms do not hold true and a formal metadata may not be published for a consumer to discover and generate proxies.
For calling a REST service you'd need to manually create a proxy that reaches the service. You can use classes like HttpWebRequest/WebClient for same.

Upvotes: 0

Tim
Tim

Reputation: 28530

To add and use the reference to your service library, you need to add a reference to the service library assembly in your Windows Service project, and then add the using RestfulService statement to the using directives in your Windows Service code.

Also, since you're wanting to use REST, I'd recommend using WebServiceHost instead of ServiceHost:

using RestfulService;

public partial class Service1 : ServiceBase
{

    public Service1()
    {
        InitializeComponent();
    }

    WebServiceHost sHost;

    protected override void OnStart(string[] args)
    {
        try
        {
            sHost = new WebServiceHost(typeof(RestfulService.Service1));
            sHost.Open();
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry(ex.Message);
        }
    }

    protected override void OnStop()
    {
        sHost.Close();
    }
}    

Upvotes: 1

Balu
Balu

Reputation: 39

By default rest services(webhttp bindings) are not supported in adding references. If you want to add the reference you can add one soap endpoint and then try to add the reference. Then it will work.

if you want to make a call to restful service then you can do like this

protected void Page_Load(object sender, EventArgs e)
        {   
WebRequest request = WebRequest.Create("https://192.168.1.118/PracticeWcfService1/Service1.svc/RestTypeWithSecure/GetProductData");
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
                WebResponse ws = request.GetResponse();
        string text;
using (var sr = new StreamReader(ws.GetResponseStream()))
            {
                text = sr.ReadToEnd();
}

        Response.write(text );
}

Upvotes: -1

Related Questions