Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Indigo

"Generic" indigo console host

 
 
Thread Tools Display Modes
Old 03-04-2006   #1 (permalink)
Damon Allison
Guest


 

"Generic" indigo console host

Hi,

Has anyone written a host that will read the config file at runtime and
create a service host instance for all services/endpoints in
app.config? If so, please point me to the code. If not, how would I
go about it? I see there is a ServiceModel.Configuration namespace -
could I use that?

Thanks,
Damon
Old 03-04-2006   #2 (permalink)
Juval Lowy
Guest


 

Re: "Generic" indigo console host

Hello Damon,

It is not a big deal writing one, but then you will not be able to set base
addresses, and that will imply full addresses only in the config. In addition,
you will not be able to get to some other features like singleton instance
setting, throttling, etc.

thanks,

Juval Lowy.

> Hi,
>
> Has anyone written a host that will read the config file at runtime
> and
> create a service host instance for all services/endpoints in
> app.config? If so, please point me to the code. If not, how would I
> go about it? I see there is a ServiceModel.Configuration namespace -
> could I use that?
>
> Thanks,
> Damon



Old 03-04-2006   #3 (permalink)
Ido Samuelson
Guest


 

Re: "Generic" indigo console host

Hello Juval,

This should help you doing it. I removed some validation methods and such,
since I did it fast to post it here.
Though I plan to post a better solution on my blog. I'll post a url when
completed.

PS.
This solution uses a services.xml to read which service types to load.
On a project am working on we basically recieving all the services needed
to load from a configuration service :-)

You will still have all the app.config capabilites, since this solution still
uses the app.config.
If you want something else you can create a derived ServiceHost and override
the ApplyConfiguration method.


class HostManager
{
private static List<ServiceHost> serviceHosts; // list of loaded
sericeHosts

private static void InitServices()
{
ServiceList serviceList = GetServiceList();
Trace.WriteLine("Opening services...");
foreach (ServiceData data in serviceList.Services)
{
try
{
OpenService(data);
}
catch (Exception ex)
{
string msg = string.Format("Error Loading ServiceHost
{0}, {1}", data.AssemblyName, data.TypeName);
Trace.WriteLine(msg);
Trace.WriteLine(ex.Message);
}
}
}

private static void OpenService(ServiceData data)
{
ServiceHost host = CreateServiceHost(data) as ServiceHost;
host.SetConfiguration(data.EndPoints);
serviceHosts.Add(host);
host.Open();
}

private static ServiceList GetServiceList()
{
ServiceList list= null;
XmlFormatter formatter = new XmlFormatter();
if (File.Exists(@"services.xml"))
{
using (StreamReader sr = new StreamReader(@"services.xml"))
{
list = formatter.Deserialize<ServiceList>(sr.BaseStream);
}
}
else
{
ServiceData data = new ServiceData();
ServiceEndPointData ep = new ServiceEndPointData();
ep.Address = "http://localhost:8000/EchoService";
ep.Binding = "wsHttpBinding";
ep.Contract = "Tests.EchoService.EchoContract";
List<ServiceEndPointData> eps = new List<ServiceEndPointData>();
eps.Add(ep);
data.EndPoints = eps.ToArray();
Trace.WriteLine("Creating default xml file.");
data.AssemblyName = "Tests.EchoService";
data.TypeName = "Tests.EchoService.EchoServiceHost";
list = new ServiceList();
List<ServiceData> servicesList = new List<ServiceData>();
servicesList.Add(data);
list.Services = servicesList.ToArray();
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"services.xml"))
{
sw.AutoFlush = true;
formatter.Serialize(sw.BaseStream, list);
}
}
return list;
}

private static ServiceHost CreateServiceHost(ServiceData data)
{
ObjectHandle handle = Activator.CreateInstance(data.AssemblyName,
data.TypeName);
ServiceHost host = handle.Unwrap() as ServiceHost;
return host;
}
}

[DataContract(Namespace=Namespaces.ServiceData)]
public class ServiceData
{
[DataMember]
public string AssemblyName;

[DataMember]
public string TypeName;

}

[DataContract(Namespace=Namespaces.ServiceListResponse)]
public class ServiceListResponse
{
[DataMember]
public ServiceData[] Services;
}



Best Regards,

Ido Samuelson
Senior consultant
Advantech (Magen MTC)
mailto:idos@magen.com

> Hello Damon,
>
> It is not a big deal writing one, but then you will not be able to set
> base addresses, and that will imply full addresses only in the config.
> In addition, you will not be able to get to some other features like
> singleton instance setting, throttling, etc.
>
> thanks,
>
> Juval Lowy.
>
>> Hi,
>>
>> Has anyone written a host that will read the config file at runtime
>> and
>> create a service host instance for all services/endpoints in
>> app.config? If so, please point me to the code. If not, how would
>> I
>> go about it? I see there is a ServiceModel.Configuration namespace -
>> could I use that?
>> Thanks,
>> Damon



Old 03-04-2006   #4 (permalink)
Ido Samuelson
Guest


 

Re: "Generic" indigo console host

In the code I post, I basically load Dynamically derived service hosts and
not services.
This could easily be changed though. Sorry for not pointed that out.

Best Regards,

Ido Samuelson
Senior consultant
Advantech (Magen MTC)
mailto:idos@magen.com

> Hello Juval,
>
> This should help you doing it. I removed some validation methods and
> such, since I did it fast to post it here. Though I plan to post a
> better solution on my blog. I'll post a url when completed.
>
> PS. This solution uses a services.xml to read which service types to
> load. On a project am working on we basically recieving all the
> services needed to load from a configuration service :-)
>
> You will still have all the app.config capabilites, since this
> solution still uses the app.config. If you want something else you can
> create a derived ServiceHost and override the ApplyConfiguration
> method.
>
> class HostManager
> {
> private static List<ServiceHost> serviceHosts; // list of
> loaded
> sericeHosts
> private static void InitServices()
> {
> ServiceList serviceList = GetServiceList();
> Trace.WriteLine("Opening services...");
> foreach (ServiceData data in serviceList.Services)
> {
> try
> {
> OpenService(data);
> }
> catch (Exception ex)
> {
> string msg = string.Format("Error Loading
> ServiceHost
> {0}, {1}", data.AssemblyName, data.TypeName);
> Trace.WriteLine(msg);
> Trace.WriteLine(ex.Message);
> }
> }
> }
> private static void OpenService(ServiceData data)
> {
> ServiceHost host = CreateServiceHost(data) as ServiceHost;
> host.SetConfiguration(data.EndPoints);
> serviceHosts.Add(host);
> host.Open();
> }
> private static ServiceList GetServiceList()
> {
> ServiceList list= null;
> XmlFormatter formatter = new XmlFormatter();
> if (File.Exists(@"services.xml"))
> {
> using (StreamReader sr = new
> StreamReader(@"services.xml"))
> {
> list =
> formatter.Deserialize<ServiceList>(sr.BaseStream);
> }
> }
> else
> {
> ServiceData data = new ServiceData();
> ServiceEndPointData ep = new ServiceEndPointData();
> ep.Address = "http://localhost:8000/EchoService";
> ep.Binding = "wsHttpBinding";
> ep.Contract = "Tests.EchoService.EchoContract";
> List<ServiceEndPointData> eps = new
> List<ServiceEndPointData>();
> eps.Add(ep);
> data.EndPoints = eps.ToArray();
> Trace.WriteLine("Creating default xml file.");
> data.AssemblyName = "Tests.EchoService";
> data.TypeName = "Tests.EchoService.EchoServiceHost";
> list = new ServiceList();
> List<ServiceData> servicesList = new
> List<ServiceData>();
> servicesList.Add(data);
> list.Services = servicesList.ToArray();
> using (System.IO.StreamWriter sw = new
> System.IO.StreamWriter(@"services.xml"))
> {
> sw.AutoFlush = true;
> formatter.Serialize(sw.BaseStream, list);
> }
> }
> return list;
> }
> private static ServiceHost CreateServiceHost(ServiceData data)
> {
> ObjectHandle handle =
> Activator.CreateInstance(data.AssemblyName,
> data.TypeName);
> ServiceHost host = handle.Unwrap() as ServiceHost;
> return host;
> }
> }
> [DataContract(Namespace=Namespaces.ServiceData)]
> public class ServiceData
> {
> [DataMember]
> public string AssemblyName;
> [DataMember]
> public string TypeName;
> }
>
> [DataContract(Namespace=Namespaces.ServiceListResponse)]
> public class ServiceListResponse
> {
> [DataMember]
> public ServiceData[] Services;
> }
> Best Regards,
>
> Ido Samuelson
> Senior consultant
> Advantech (Magen MTC)
> mailto:idos@magen.com
>> Hello Damon,
>>
>> It is not a big deal writing one, but then you will not be able to
>> set base addresses, and that will imply full addresses only in the
>> config. In addition, you will not be able to get to some other
>> features like singleton instance setting, throttling, etc.
>>
>> thanks,
>>
>> Juval Lowy.
>>
>>> Hi,
>>>
>>> Has anyone written a host that will read the config file at runtime
>>> and
>>> create a service host instance for all services/endpoints in
>>> app.config? If so, please point me to the code. If not, how would
>>> I
>>> go about it? I see there is a ServiceModel.Configuration namespace
>>> -
>>> could I use that?
>>> Thanks,
>>> Damon



 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where does "Console.WriteLine" output go? Curious .NET General 3 6 Days Ago 12:12 PM
Is it possible to install a Vista "Recovery Console" in the boot menu, as in WinXP? Juan I. Cahis Vista General 7 07-19-2007 10:32 AM
Vista cannot find a suitable driver for a "Generic Serial" port Juan I. Cahis Vista General 6 06-18-2007 05:28 PM
Info : "Console based intellisense" Tabcompletion for PowerShell /\\/\\o\\/\\/ [MVP] PowerShell 2 04-22-2007 05:05 PM
"<SPACE> next page; <CR> next line; Q quit" not cleared in console Jon Davis PowerShell 2 05-30-2006 12:44 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50