2

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();
    }
}
}
raring sunny
  • 191
  • 1
  • 3
  • 16
  • 1
    type wcftextclient in visual studio command prompt. and use configuration provided in that to share in console app. or use add Service refrence utility in console App. – JSJ May 14 '15 at 07:59
  • Thanks for a quick response and a tip. Could you go through the question once again as I am having a few other issues. – raring sunny May 14 '15 at 08:08
  • 1
    right click on you app.config and select Edit WCF Configuration. by the way if you add new item under you console app as WCF Service it will add default configuration as BasicHttp etc. – JSJ May 14 '15 at 08:11
  • 1
    Class libraries (WCF or otherwise) do not use the app.config included in the project - they use the config file of the application using that library. You will need to copy the `` section to the app.config of your console application. .svc files are only required for IIS-hosted services (and you can use fileless activation to avoid having a physical svc file). The service name in the config needs to match the service name in the .svc file's code-behind. – Tim May 14 '15 at 08:15
  • Thanks @Tim. I accidently deleted the app.config file from my service library project. Any way that I can generate it again using some tool (either command line or graphical)? I would want to avoid typing in all the configuration manually. – raring sunny May 14 '15 at 08:18
  • @JSJ - Could you pls clarify on your suggestion about adding a new item under console app as WCF service? I tried adding a new file but I didn't find any WCF service template in the console app. Also, you mentioned about wcftextclient but I didn't find any such command line utility. – raring sunny May 14 '15 at 08:23
  • Right Click on you project and select add new item. then select WCF Service. it wil add new service with default DoWork Method and some basic Configurations in your App.Config File. – JSJ May 14 '15 at 08:29

3 Answers3

1

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.

tom redfern
  • 30,562
  • 14
  • 91
  • 126
0

And do you strictly need to edit App.Config in order to define/edit service configuration?

Rather than that, I would suggest you doing it programmatically - check for example this link. Im not sure whether you want it for creating new endpoint or just changing the settings, but still, it can be done like that.

JakubJ
  • 235
  • 4
  • 13
  • Thanks for your response. I would prefer going with the configuration first instead of programatically doing it. Since I am doing this just to have a better understanding, I would prefer going with a config file. I have added some more information to my question since I am facing few other issues with app.config. Take a look when you get a chance. – raring sunny May 14 '15 at 08:12
0

Its not mandetory to have svc file while using self hosting. it being helpfull for use outer world just like asmx files. my suggestion is to use the default configuration as refrence and create your own configs. as these consist of Address where service is geting hosted. Binding what kind of binding you want to use. and Contract what type of contract you wish to use.

Using WCFTestClient / Edit WCF Configuration utility will help you here.

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

http://www.c-sharpcorner.com/UploadFile/Mahadesh/wcf-series-using-the-wcf-service-configuration-editor/

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

JSJ
  • 5,653
  • 3
  • 25
  • 32