0

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.

Tito Rahman
  • 107
  • 2
  • 9
  • 1
    for starters, your contract should be bound to the interface, not a specific method. Change `contract="hessian.test.HessianService.sayHello"` to `contract="hessian.test.testInterface"` and that should get you a little closer. – ethorn10 Sep 16 '14 at 04:14

1 Answers1

2

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:

  • applying ServiceContract and OperationContract to the service and operation declarations respectively
  • applying WebGet attribute to the operation so that it'll respond to a GET request
  • configuring service and behaviors so WCF can read them and handle things appropriately

WCF 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

Community
  • 1
  • 1
Allen Zeng
  • 2,635
  • 2
  • 20
  • 31
  • I'm looking to make it by editing web.conf. If I'm not mistaken, that example just shows how to use visual studios to make an API. I'm more interested in how it works. – Tito Rahman Sep 16 '14 at 01:50
  • I'm not entirely sure what you mean by "making it by editing web.conf". Whatever you do, you'll need to have a host for your service, since you've explicitly said you don't want to use IIS, one way to host it is to [write your own hosting application](http://msdn.microsoft.com/en-us/library/ms731758(v=vs.110).aspx) – Allen Zeng Sep 16 '14 at 01:57
  • I do want to use **IIS** to host. I just don't want to use any of the "Magic" such as using .asmx files to create a service. By "making it by editing", I mean declare a service in web.conf manually like the way I have done in my code. I just want to figure out how to get it to work. – Tito Rahman Sep 16 '14 at 02:08