无法在 Windows 服务中添加 RESTful WCF 服务的引用

本文关键字:服务 RESTful WCF 引用 添加 Windows | 更新日期: 2023-09-27 18:32:10

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

我的界面是

[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);
        }

在窗口中托管

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 服务的引用

无法在 Windows 服务中添加 RESTful WCF 服务的引用

若要添加和使用对服务库的引用,需要在 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();
    }
}    

你通过重叠的 SOAP 和 REST 标准来混淆自己。
RESTful 风格的服务不遵循 SOAP 标准。VS 中的"添加引用"功能下载基于 SOAP 的服务的元数据(包括 WSDL),以了解其协定/绑定等。但是,在基于 REST 的服务的情况下,这些标准/机制不成立,并且可能不会发布正式的元数据供消费者发现和生成代理。要调用 REST 服务,您需要手动创建访问该服务的代理。您可以使用像HttpWebRequest/WebClient这样的类来实现相同的目的。

默认情况下,

添加引用不支持 rest 服务(webhttp 绑定)。如果要添加引用,可以添加一个 SOAP 终结点,然后尝试添加引用。然后它就会起作用。

如果您想拨打 Restful Service 的电话,那么您可以这样做

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 );
}