Nimit Joshi
Nimit Joshi

Reputation: 1046

Creating WCF Service

I am new in WCF. I have created a wcf service application and in it there is an interface and class.

My Interface:

using System.ServiceModel;

namespace WcfService1
{   
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string Display();        
    }    
}

My class:

namespace WcfService1
{
    public class Service1 : IService1
    {
        public string Display()
        {
            return "Hello";
        }
    }
}

Now when i am running this service in the browser. The following screen is coming after clicking on the Service1.svc

enter image description here

Ho do I access the Display() on the browser?

After followed the first answer, the following is coming:

enter image description here

Upvotes: 1

Views: 417

Answers (2)

Ramie
Ramie

Reputation: 1201

I think you're looking for WebGet if you want to access it through your browser.

using System.ServiceModel;
using System.ServiceModel.Web;

namespace WcfService1
{   
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(RequestFormat = WebMessageFormat.XML, ResponseFormat = WebMessageFormat.XML, UriTemplate = "/display/")]
        string Display();        
    }    
}

Add these behaviours

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

Add this endpoint address

  <service behaviorConfiguration="ServiceBehaviour" name="WCFService1.Service1">
      <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
      bindingConfiguration="b_WebHttpBinding" contract="WCFService1.IService1" />
  </service>

Add this binding

<bindings>
  <webHttpBinding>
    <binding name="b_WebHttpBinding" closeTimeout="00:01:00"
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
        maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Buffered"
        useDefaultWebProxy="true" crossDomainScriptAccessEnabled="true">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
          maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
            realm="" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>

Now you should be able to call your service like

http://localhost:22727/Service1.svc/display/

Then it should display a string in XML Format (you can change it to JSON if you like)

Also, make sure you reference the following libraries

System.ServiceModel.Web
System.Web.Extensions
System.Web.Services
System.Web

http://msdn.microsoft.com/en-us/library/bb412172(v=vs.110).aspx

Upvotes: 2

Marius George
Marius George

Reputation: 526

Split your Solution into two projects:

  • New Project -> WCF -> WCF Service Library
  • New Project -> WCF -> WCF Service Application

The "Service Application" is your web project containing the svc endpoint. The "Service Library" is a class library containing your services.

When you run (F5) the service library project, Visual Studio will start the WCF Test Client (WcfTestClient.exe), giving you a quick interface to test your services.

The .svc endpoint in your web project does not give you a built-in test interface (similar to the way it worked in the asmx days).

Upvotes: 0

Related Questions