How do I switch from named endpoints to .config endpoints

Hello,

I've been working on using a windows service project to host my service. (code of OnStart below). Using the MS Press "Windows Communication Foundatation - Step by Step" book's example. I've been attempting to take the named endpoints and use an App.Config to house them but I can't seem to get the hosting service to read the config?

Any suggestion on how to do this?

John

protectedoverridevoid OnStart(string[] args)

{

// Start service.

productsServiceHost =newServiceHost(typeof(ProductsServiceImpl));

// new Uri("net.pipe:"), new Uri("http:"), new Uri("net.tcp:"));

NetNamedPipeBinding binding1 =newNetNamedPipeBinding();

productsServiceHost.AddServiceEndpoint(typeof(IProductsService),

binding1,"net.pipe://localhost/ProductsServicePipe");

NetTcpBinding binding2 =newNetTcpBinding();

productsServiceHost.AddServiceEndpoint(typeof(IProductsService),

binding2,"net.tcp://localhost:8080/ProductsServicePipe");

BasicHttpBinding binding3 =newBasicHttpBinding();

productsServiceHost.AddServiceEndpoint(typeof(IProductsService),

binding3,"http://<hostname>/ProductsService/ProductsService.svc");

productsServiceHost.Open();

}

[2080 byte] By [JohnC#] at [2008-1-5]
# 1
Check out http://msdn2.microsoft.com/en-us/library/ms734786.aspx and the nearby topics.
BrianMcNamara-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 2

When I tried this the service fails to start, I edited the config to fit my application (see below) and remmed out the fixed end points I had been using.

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

</configSections>

<dataConfiguration defaultDatabase="dbConnection" />

<connectionStrings>

<add name="dbConnection" connectionString="Database=<dbNameHere>;Server=(local)\SQLEXPRESS;Integrated Security=SSPI"

providerName="System.Data.SqlClient" />

</connectionStrings>

<appSettings>

<!-- use appSetting to configure base address provided by host -->

<add key="baseAddress" value="http://localhost:8000/DASvc" />

</appSettings>

<system.serviceModel>

<services>

<service

name="Name"

behaviorConfiguration="DASvcTestServiceBehavior">

<host>

<baseAddresses>

<add baseAddress="http://localhost:8000/DASvc"/>

</baseAddresses>

</host>

<endpoint address="http://<hostname here>:8000/DASvc/DASvcTest.svc"

binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IProduct"

contract="DASvcClient.DASvc.IProduct"

name="BasicHttpBinding_IProduct" />

<endpoint address="net.pipe://localhost/ProductsServicePipe"

binding="netNamedPipeBinding" bindingConfiguration="" contract="DASvcClient.DASvc.IProduct"

name="NetNamedPipeBinding_IProduct" />

<endpoint address="net.tcp://localhost:8080/ProductsServicePipe"

binding="netTcpBinding" bindingConfiguration="" contract="DASvcClient.DASvc.IProduct"

name="NetTcpBinding_IProduct" />

</service>

</services>

<!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->

<behaviors>

<serviceBehaviors>

<behavior name="DASvcTestServiceBehavior">

<serviceMetadata httpGetEnabled="True"/>

<serviceDebug includeExceptionDetailInFaults="False" />

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration>

JohnC# at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 3

By 'fails to start', what do you mean? Is there an exception? Or does nothing come up when you hit the base address in a web browser?

It doesn't look like your <service name=...> is the same as the type of your class you passed to the ServiceHost constructor - these need to match, so that may be the issue.

And just to double check, you commented out the endpoints, but kept the 'new ServiceHost' and 'host.Open()' lines of code, yes?

BrianMcNamara-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 4

Here's the OnStart code now (Below) and by fails to start I mean that the Services applet under the Administrative Tools on the windows control panel is being used to start/stop this service (a Windows Service project).

The service gets started and the finally a console app attempts to use it.

I removed the RiskAgility... from the service name and left just the last value (name of the service?).

protectedoverridevoid OnStart(string[] args)

{

// Start the service

dasvcHostController = newServiceHost(typeof(ProductSvcImpl));

//NetNamedPipeBinding binding1 = new NetNamedPipeBinding();

//dasvcHostController.AddServiceEndpoint(typeof(IProduct),

// binding1, "net.pipe://localhost/ProductsServicePipe");

//NetTcpBinding binding2 = new NetTcpBinding();

//dasvcHostController.AddServiceEndpoint(typeof(IProduct),

// binding2, "net.tcp://localhost:8080/ProductsServicePipe");

//BasicHttpBinding binding3 = new BasicHttpBinding();

//dasvcHostController.AddServiceEndpoint(typeof(IProduct),

// binding3, "http://<hostname here>:8000/DASvc/DASvcTest.svc");

dasvcHostController.Open();

}

App.Config looks like this now:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

</configSections>

<dataConfiguration defaultDatabase="dbConnection" />

<connectionStrings>

<add name="dbConnection" connectionString="Database=<dbName>;Server=(local)\SQLEXPRESS;Integrated Security=SSPI"

providerName="System.Data.SqlClient" />

</connectionStrings>

<system.serviceModel>

<bindings>

<basicHttpBinding>

<binding name="BasicHttpBinding_IProduct" closeTimeout="00:01:00"

openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"

allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"

maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"

messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"

useDefaultWebProxy="true">

<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

maxBytesPerRead="4096" maxNameTableCharCount="16384" />

<security mode="None">

<transport clientCredentialType="None" proxyCredentialType="None"

realm="" />

<message clientCredentialType="UserName" algorithmSuite="Default" />

</security>

</binding>

</basicHttpBinding>

</bindings>

<services>

<service behaviorConfiguration="DASvcTestServiceBehavior" name="DASvcTest">

<endpoint address="http://<hostname>:8000/DASvc/DASvcTest.svc"

binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IProduct"

name="BasicHttpBinding_IProduct" contract="DASvcTest.IProduct" />

<endpoint address="net.pipe://localhost/ProductsServicePipe"

binding="netNamedPipeBinding" bindingConfiguration="" name="NetNamedPipeBinding_IProduct"

contract="DASvcTest.IProduct" />

<endpoint address="net.tcp://localhost:8080/ProductsServicePipe"

binding="netTcpBinding" bindingConfiguration="" name="NetTcpBinding_IProduct"

contract="DASvcTest.IProduct" />

<host>

<baseAddresses>

<add baseAddress="tcp://<hostname>:8088/ProductsServicePipe" />

</baseAddresses>

</host>

</service>

</services>

<!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->

<behaviors>

<serviceBehaviors>

<behavior name="DASvcTestServiceBehavior">

<serviceMetadata httpGetEnabled="True"/>

<serviceDebug includeExceptionDetailInFaults="False" />

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration>

And the Service class itself looks like this:

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

using System.Runtime.Serialization;

using Microsoft.Practices.EnterpriseLibrary.Data;

using System.Data;

namespace <namespace>

{

//Data Contract for AssetAttributes

[DataContract]

publicclassAssetAttributes

{

[DataMember]

publicInt32 AssetID;

[DataMember]

publicDateTime upsize_ts;

}

//Data contract for

[DataContract]

publicclassAssetInventory

{

[DataMember]

publicInt32 AssetClassID;

[DataMember]

publicstring AssetClassName;

}

//Data Contract for Product

[DataContract]

publicclassProduct

{

[DataMember]

publicint ProdID;

[DataMember]

}

[ServiceContract]

publicinterfaceIProduct

{

//Get Product by product ID

[OperationContract]

Product GetProductByID(int iProductID);

//[OperationContract]

[OperationContract]

List<string> GetAllProducts();

//Get AssetInventory

[OperationContract]

List<AssetInventory> GetAllAssetInventory();

//Get AssetInventory

[OperationContract]

List<AssetAttributes> GetAllAssetAttributes();

}

//[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

publicclassProductSvcImpl : IProduct

{

#region IProduct Members

publicProduct GetProductByID(int iProductID)

{

thrownewException("The method or operation is not implemented.");

}

publicList<string> GetAllProducts()

{

try

{

Database <namespace> = DatabaseFactory.CreateDatabase("dbConnection");

string sqlQuery = @"SELECT ProdID FROM Product";

IDataReader productReader = <namespace>.ExecuteReader(CommandType.Text, sqlQuery);

List<string> products = newList<string>();

while (productReader.Read())

{

products.Add(productReader.GetString(10));

}

return products;

}

catch (Exception ex)

{

//throw new Exception("The method or operation is not implemented.");

List<string> products = newList<string>();

products.Add(ex.Message);

return products;

}

}

publicList<AssetInventory> GetAllAssetInventory()

{

try

{

Database <dbName> = DatabaseFactory.CreateDatabase("dbConnection");

string sqlQuery = @"SELECT [AssetClassID], [AssetClassName]

FROM [<dbName>].[dbo].[AssetInventory]

WHERE [ScenarioID] = 1";

IDataReader dbReader = <dbName>.ExecuteReader(CommandType.Text, sqlQuery);

List<AssetInventory> assetInventoryItems = newList<AssetInventory>();

while (dbReader.Read())

{

AssetInventory assetInventoryItem = newAssetInventory();

assetInventoryItem.AssetClassID = dbReader.GetInt32(0);

assetInventoryItem.AssetClassName = dbReader.GetString(1);

assetInventoryItems.Add(assetInventoryItem);

}

dbReader.Close();

return assetInventoryItems;

}

catch

{

//throw new Exception("The method or operation is not implemented.");

List<AssetInventory> assetInventories = newList<AssetInventory>();

return assetInventories;

}

}

publicList<AssetAttributes> GetAllAssetAttributes()

{

try

{

Database <DBName> = DatabaseFactory.CreateDatabase("dbConnection");

string sqlQuery = @"SELECT [AssetID]

FROM [<dbName>].[dbo].[AssetAttributes]

WHERE [ContPorID] = 2";

IDataReader dbReader = <DBName>.ExecuteReader(CommandType.Text, sqlQuery);

List<AssetAttributes> assetAttributeItems = newList<AssetAttributes>();

while (dbReader.Read())

{

AssetAttributes assetAttributeItem = newAssetAttributes();

assetAttributeItem.AssetID = dbReader.GetInt32(0);

assetAttributeItems.Add(assetAttributeItem);

}

dbReader.Close();

return assetAttributeItems;

}

catch

{

//throw new Exception("The method or operation is not implemented.");

List<AssetAttributes> assetAttributeItems = newList<AssetAttributes>();

AssetAttributes errorAssetAttribute = newAssetAttributes();

errorAssetAttribute.AssetID = 0;

errorAssetAttribute.AssetLabel = "No Data found or unknown error occured";

assetAttributeItems.Add(errorAssetAttribute);

return assetAttributeItems;

}

}

#endregion

}

}

JohnC# at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 5
Why don't "DASvcTest" in the <service> config, and "ProductSvcImpl" in the "new ServiceHost" code match? Does it work if you change the <service name> to "DASvcTest.ProductSvcImpl"?
BrianMcNamara-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 6

Okay, now the OnStart looks like this:

// Start the service

dasvcHostController = newServiceHost(typeof(DASvcTest.ProductSvcImpl));

//NetNamedPipeBinding binding1 = new NetNamedPipeBinding();

//dasvcHostController.AddServiceEndpoint(typeof(IProduct),

// binding1, "net.pipe://localhost/ProductsServicePipe");

//NetTcpBinding binding2 = new NetTcpBinding();

//dasvcHostController.AddServiceEndpoint(typeof(IProduct),

// binding2, "net.tcp://localhost:8080/ProductsServicePipe");

//BasicHttpBinding binding3 = new BasicHttpBinding();

//dasvcHostController.AddServiceEndpoint(typeof(IProduct),

// binding3, "http://<hostname>:8000/DASvc/DASvcTest.svc");

dasvcHostController.Open();

And the App.Config looks like this:

<service behaviorConfiguration="DASvcTestServiceBehavior" name="DASvcTest.ProductSvcImpl">

<endpoint address="http://<hostname>:8000/DASvc/DASvcTest.svc"

binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IProduct"

name="BasicHttpBinding_IProduct" contract="DASvcTest.IProduct" />

They match and when I attempt to start the service in the Service applet it says in a dialog: "The DASvcTest service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service."

JohnC# at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 7

Okay, I solved the problem, it was in the App.Config file. The section below specifying the bassAddress was needed.

I still have yet to figure out why this worked.

<host>

<baseAddresses>

<add baseAddress=http://<hostname here>:8000/DASvc/DASvcTest.svc />

</baseAddresses>

</host>

JohnC# at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...

Visual Studio Orcas

Site Classified