Reputation: 107
I am fairly new to the C# world so I don't know much. I can't even find simple step by step documentation on how to set up a simple service without using the built in templates in Visual Studios.
I would prefer to use the following class and web.conf to make my service. I do not want to use anything that is going to depend on visual studios or IIS magic like .asmx files.
I can't seem to get my server to respond to it. When i go to localhost:8152/02/service or localhost:8152/02/service/echo2, I get a 404 error.
I have the following in my web.conf file.
<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>
This is in my .cs file
namespace hessian.test{
public class HessianService : WebService, testInterface
{
public void runVoid(int count)
{
}
public string sayHello()
{
return "Hello";
}
public string repeatMe(string s)
{
return s;
}
}
}
Any help would be appreciated.
Upvotes: 0
Views: 162
Reputation: 2665
I suggest taking a look at Getting Started with WCF. WCF operates with .svc files instead of .asmx. Here's a comparison.
In your example you'll need to create contracts like so:
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfService1
{
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "sayhello")]
Stream SayHello();
}
}
Then an implementation can look like this:
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"));
}
}
}
And of course, the all important web.config
, notice the serviceHostingEnvironment
element, it is required if you don't want to create a .svc file, although a .svc file doesn't require IIS, you can host it anywhere.
<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>
There are quite a few things you need to get right before the service can even work:
ServiceContract
and OperationContract
to the service and operation declarations respectively WebGet
attribute to the operation so that it'll respond to a GET
requestservice
and behaviors
so WCF can read them and handle things appropriatelyWCF is powerful but it's also quite a bit to take in, which was why I suggested WebApi at first. It has a much more gentle learning curve, assuming you want to use REST as opposed to SOAP. There are also alternatives like NancyFx and ServiceStack
Upvotes: 2