WCF Service in a console application

Sometimes you just want to create a little console application so you can kickstart your development.
I will explain how to do this in the following post.

Create a new console appplication
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Add Reference.... Select System.ServiceModel from the .NET tab and click OK.
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCFConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Service1));
            host.Open();
            Console.Write("Service is up and running");
            Console.ReadKey();
            host.Close();
        }
    }

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GreetingMessage(string Name);
    }

    public class Service1 : IService1
    {
        public string GreetingMessage(string name)
        {
            return "Welcome to WCF " + name;
        }
    }
}

Add App.config:


<!-- Service Endpoints -->

<!--
              Upon deployment, the following identity element should be removed or replaced to reflect the
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
              automatically.
          -->

<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

<!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->

soapUI Compatible App.config:
Remember to activate WS-A options in your request:
Add default wsa:Action
Add default wsa:To

<configuration>
  <system .servicemodel=".servicemodel">
    <!-- Start soapUI compatibility -->
    <bindings>
      <wshttpbinding>
        <binding name="wsHttpBindingNoSCT">
          <security mode="None">
            <transport clientcredentialtype="None">
            </transport>
          </security>
        </binding>
      </wshttpbinding>
    </bindings>

    <!-- Stop soapUI compatibility -->

    <services>
      <service behaviorconfiguration="WCFConsole.Service1Behavior" name="WCFConsole.Service1">
        <!-- Service Endpoints -->
        <endpoint address="" binding="wsHttpBinding" bindingconfiguration="wsHttpBindingNoSCT" contract="WCFConsole.IService1">
          <!-- Upon deployment, the following identity element should be removed or replaced to reflect the
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
          automatically.
          -->
          <identity>
            <dns value="win-2008r2">
            </dns>
          </identity>
        </endpoint>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
          <host>
            <baseaddresses>
              <add baseaddress="http://win-2008r2:8181/Service1.svc">
              </add>
            </baseaddresses>
          </host>
        </endpoint>
      </service>
    </services>

    <behaviors>
      <servicebehaviors>
        <behavior name="WCFConsole.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <servicemetadata httpgetenabled="true">
            <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
            <servicedebug includeexceptiondetailinfaults="false">
            </servicedebug>
          </servicemetadata>
        </behavior>
      </servicebehaviors>
    </behaviors>
  </system>

  <connectionstrings>
    <add connectionstring='metadata=res://*/TMSEDEV_CustomerMasterINT56.csdl|res://*/TMSEDEV_CustomerMasterINT56.ssdl|res://*/TMSEDEV_CustomerMasterINT56.msl;provider=System.Data.SqlClient;provider connection string="data source=.sqlexpress;initial catalog=TMSEDEV_CustomerMasterINT56;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"' name="TMSEDEV_CustomerMasterINT56Entities" providername="System.Data.EntityClient"></add>
  </connectionstrings>

</configuration>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s