Search code examples
wcfconsole-applicationapp-configservicehost

WCF Service Host using Console App


Is there a way to automatically add entries to the App.config of a console application that will be used a host for a WCF service or do I need to make those entries manually?

I am referring to the <system.serviceModel> section in App.config file to configure a WCF service.

Additional issues with Service library project's app.config

I understand the concept of .svc files and that those are needed only in case you want to host a service using IIS. To start with, I plan to self-host a service using cosole app. Now another issue that I am facing is with the web.config of my service library project. When I created the service library project, an app.config was automatically created and the service configuration was automatically generated. However, I had changed the class names and now I see an error in the app.config for <service name="MyServiceName"> parameter and also the an error on Service contract attribute.

What's wrong in the following configuration? I get an error with service name="ContactMgrService.ContactManager" and contract="ContactMgrService.IContactMgrService"

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="ContactMgrService.ContactManager">
            <endpoint address="" binding="basicHttpBinding" contract="ContactMgrService.IContactMgrService">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8733/Design_Time_Addresses/ContactMgrService/Service1/" />
                </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

Service Contract

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ContactMgrService.DataModel.Contact;

namespace ContactMgrService.Contact
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IContactMgrService
{
    [OperationContract]
    IList<ContactData> GetContactList(int? value);

}

}

Service Implementation class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ContactMgrService.DataModel.Contact;

namespace ContactMgrService.Contact
{
public class ContactManager : IContactMgrService
{
    ContactContext db = new ContactContext();


    public IList<ContactData> GetContactList(int? value)
    {
        IQueryable<ContactData> contacts = db.Contacts;

        return contacts.ToList();
    }
}
}

Solution

  • Your config file is incorrect. When specifying service name and endpoint contract values you need to specify the fully qualified name of the type containing the service or service definition:

    <service name="ContactMgrService.Contact.ContactManager">
    

    and

    <endpoint address="" 
              binding="basicHttpBinding"
              contract="ContactMgrService.Contact.IContactMgrService">
    

    In response to your other questions:

    Is it mandatory to have a project with svc files?

    It is highly unorthodox if not impossible to use svc files in a console applicaiton. This is strinctly for IIS.

    Is there a way to automatically add entries to the App.config

    Nope you have to do this manually.