如何使用web.conf在c#中创建web服务
我对C#world相当陌生,所以我知道的不多。我甚至找不到关于如何在不使用Visual Studio中内置模板的情况下设置简单服务的简单分步文档 我更愿意使用下面的类和web.conf来创建我的服务。我不想使用任何依赖visual Studio或IIS magic(如.asmx文件)的文件 我似乎无法让我的服务器响应它。当我转到localhost:8152/02/service或localhost:8152/02/service/echo2时,我得到一个404错误 我的web.conf文件中有以下内容如何使用web.conf在c#中创建web服务,c#,asp.net,.net,web-services,C#,Asp.net,.net,Web Services,我对C#world相当陌生,所以我知道的不多。我甚至找不到关于如何在不使用Visual Studio中内置模板的情况下设置简单服务的简单分步文档 我更愿意使用下面的类和web.conf来创建我的服务。我不想使用任何依赖visual Studio或IIS magic(如.asmx文件)的文件 我似乎无法让我的服务器响应它。当我转到localhost:8152/02/service或localhost:8152/02/service/echo2时,我得到一个404错误 我的web.conf文件中有以
<system.serviceModel>
<services>
<service name ="hessian.test.HessianService" behaviorConfiguration="HttpGetMetadata">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8152/02/service"/>
</baseAddresses>
</host>
<endpoint address="/echo2" contract="hessian.test.HessianService.sayHello" binding="wsHttpBinding"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name ="HttpGetMetadata">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings />
<client />
</system.serviceModel>
任何帮助都将不胜感激。我建议看一下。WCF使用.svc文件而不是.asmx进行操作。给你 在您的示例中,您需要创建如下合同:
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfService1
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "sayhello")]
Stream SayHello();
}
}
然后,实现可以如下所示:
using System.IO;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
public class Service : IService
{
public Stream SayHello()
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
return new MemoryStream(Encoding.UTF8.GetBytes("hello"));
}
}
}
当然,非常重要的web.config
,请注意serviceHostingEnvironment
元素,如果您不想创建.svc文件,则它是必需的,尽管.svc文件不需要IIS,但您可以在任何地方托管它
<system.serviceModel>
<services>
<service name="WcfService1.Service">
<endpoint address="" binding="webHttpBinding" contract="WcfService1.IService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment>
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory" relativeAddress="./sayhello.svc" service="WcfService1.Service"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
一开始假设您想使用REST而不是SOAP,那么它的学习曲线要温和得多。还有其他选择,如和我希望通过编辑web.conf来实现。如果我没有弄错的话,这个例子只是展示了如何使用VisualStudio来制作API。我更感兴趣的是它是如何工作的。我不完全确定你所说的“通过编辑web.conf来实现”是什么意思。无论您做什么,您都需要为您的服务提供一个主机,因为您明确表示不想使用IIS,托管它的一种方法是我确实想使用IIS托管。我只是不想使用任何“魔法”,比如使用.asmx文件来创建服务。“通过编辑实现”,我的意思是像我在代码中所做的那样,在web.conf中手动声明服务。我只是想弄清楚如何让它工作。对于初学者,您的合同应该绑定到接口,而不是特定的方法。将contract=“hessian.test.HessianService.sayHello”
更改为contract=“hessian.test.testInterface”
,这应该会让您更接近。
<system.serviceModel>
<services>
<service name="WcfService1.Service">
<endpoint address="" binding="webHttpBinding" contract="WcfService1.IService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment>
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory" relativeAddress="./sayhello.svc" service="WcfService1.Service"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>