【发布时间】:2013-09-24 12:39:33
【问题描述】:
当我尝试将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 服务的引用
【问题讨论】:
-
添加服务引用是指您尝试通过Visual Studio的添加服务引用添加服务吗?如果是这样,那么您不能对 RESTful 服务执行此操作;您不使用代理与 RESTful 服务进行通信,而是使用 HTTP API 直接通过 HTTP 进行调用。
-
你不能。需要使用一些服务器/客户端 api 来使用您的服务,它可以执行 http get/post,如果可以从客户端/Javascript 端调用,可以使用 jquery ajax,如果需要在 C# 端使用服务,则使用 HttpWebRequest。
-
@Tim:- 我正在添加项目作为参考
-
@LearningNeverEnds:- 我将使用 javascript 在客户端使用它。但首先我想将它托管在 Windows 服务中
-
@Gangadhar - 它的托管方式(IIS、Windows 服务、自托管)与您访问它的方式无关。如果你想在 Windows 服务中托管它,你需要编写逻辑来托管服务 - 你不需要引用服务来托管它。
标签: c# wcf rest windows-services wcf-rest