1

当我尝试将restful wcf服务的引用添加到windows服务时。我收到“找不到类型或命名空间名称'RestfulService'(您是否缺少 using 指令或程序集引用?)”错误。

我的界面是

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

应用程序配置

<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>

但我可以使用以下内容添加参考。

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

在 Windows 主机中

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()
        {
        }
    }

其中 RestfulService 是我对 wcf 服务的引用

4

3 回答 3

1

要添加和使用对服务库的引用,您需要在 Windows 服务项目中添加对服务库程序集的引用,然后将using RestfulService语句添加到 Windows 服务代码中的 using 指令中。

另外,由于您想使用 REST,我建议您使用WebServiceHost而不是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();
    }
}    
于 2013-09-19T13:12:25.717 回答
0

您被 SOAP 和 REST 的重叠标准弄糊涂了。
RESTful 风格的服务不遵循 SOAP 标准。VS 中的添加引用功能下载基于 SOAP 的服务的元数据(包括 WSDL),以了解其合约/绑定/等。但是,在基于 REST 的服务的情况下,这些标准/机制不适用,并且可能不会发布正式的元数据以供消费者发现和生成代理。
要调用 REST 服务,您需要手动创建一个可以访问该服务的代理。您可以使用类似HttpWebRequest / WebClient的类。

于 2013-09-19T19:25:11.503 回答
-1

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 );
}
于 2013-09-19T12:48:02.657 回答