当我尝试将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);
}
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>
但是我能够添加以下参考文献。
[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服务的引用
答案 0 :(得分: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();
}
}
答案 1 :(得分:0)
您通过重叠SOAP和REST标准来混淆自己。
RESTful服务风格不遵循SOAP标准。 VS中的“添加引用”功能会下载基于SOAP的服务的元数据(包括WSDL),以了解其合同/绑定/等。但是,在基于REST的服务的情况下,这些标准/机制不成立,并且可能不会发布正式元数据以供消费者发现和生成代理。
要调用REST服务,您需要手动创建到达服务的代理。您可以将HttpWebRequest / WebClient等类用于此类。
答案 2 :(得分:-1)
默认情况下,添加引用不支持休息服务(webhttp绑定)。 如果要添加引用,可以添加一个soap端点,然后尝试添加引用。然后它会工作。
如果您想要拨打休息服务,那么您可以这样做
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 );
}