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 =
new
ServiceHost(typeof
(ProductsServiceImpl));// new Uri("net.pipe:"), new Uri("http:"), new Uri("net.tcp:"));
NetNamedPipeBinding binding1 =new
NetNamedPipeBinding();productsServiceHost.AddServiceEndpoint(
typeof
(IProductsService),binding1,
"net.pipe://localhost/ProductsServicePipe"
);NetTcpBinding binding2 =new
NetTcpBinding();productsServiceHost.AddServiceEndpoint(
typeof
(IProductsService),binding2,
"net.tcp://localhost:8080/ProductsServicePipe"
);BasicHttpBinding binding3 =new
BasicHttpBinding();productsServiceHost.AddServiceEndpoint(
typeof
(IProductsService),binding3,
"http://<hostname>/ProductsService/ProductsService.svc"
);productsServiceHost.Open();
}
[2080 byte] By [
JohnC#] at [2008-1-5]
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>
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 =
new
ServiceHost(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]publicclass
AssetAttributes {
[
DataMember]public
Int32 AssetID; [DataMember]public
DateTime upsize_ts;}
//Data contract for
[
DataContract]publicclass
AssetInventory {
[
DataMember]public
Int32 AssetClassID;[
DataMember]publicstring
AssetClassName;}
//Data Contract for Product
[
DataContract]publicclass
Product {
[
DataMember]publicint
ProdID;[
DataMember] }
[
ServiceContract]publicinterface
IProduct {
//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)]
publicclass
ProductSvcImpl : 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 = new
List<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 = new
List<string
>();products.Add(ex.Message);
return
products;}
}
public
List<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 = new
List<AssetInventory>();return
assetInventories;}
}
public
List<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 = new
List<AssetAttributes>();AssetAttributes errorAssetAttribute = new
AssetAttributes();errorAssetAttribute.AssetID = 0;
errorAssetAttribute.AssetLabel =
"No Data found or unknown error occured"
;assetAttributeItems.Add(errorAssetAttribute);
return
assetAttributeItems;}
}
#endregion
}
}
Okay, now the OnStart looks like this:
// Start the service dasvcHostController =
new
ServiceHost(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."