在WCF中我有许多个Service怎样用一个host来托管呢?

在WCF中我有许多个Service怎样用一个host来托管呢?,第1张

此次[我是新手,答得不好多多包涵]

----------------------------

1)Host如果是IIS的话

添加WCF即可,默认就是Host->多WCF

2)Console或者WinService的话

Sample:

创建ConsoleApplication后,添加俩个WCFService

Service1(IService1) / Service2(IService2)

然后删除App.config(我们将手动创建Host这样更加清楚)

进入Program.cs

static void Main(string[] args)

{

try

{

Uri baseAddress = new Uri("http://localhost:8002/Service1")

ServiceHost Host = new ServiceHost(typeof(Service1), baseAddress)

Host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "Service1")

ServiceMetadataBehavior smb = new ServiceMetadataBehavior()

smb.HttpGetEnabled = true

Host.Description.Behaviors.Add(smb)

Host.Open()

Console.WriteLine("WCF1 is running at " + baseAddress.ToString())

Uri baseAddress2 = new Uri("http://localhost:8002/Service2")

ServiceHost Host2 = new ServiceHost(typeof(Service2), baseAddress2)

Host2.AddServiceEndpoint(typeof(IService2), new BasicHttpBinding(), "Service2")

ServiceMetadataBehavior smb2 = new ServiceMetadataBehavior()

smb2.HttpGetEnabled = true

Host2.Description.Behaviors.Add(smb2)

Host2.Open()

Console.WriteLine("WCF2 is running at " + baseAddress2.ToString())

Console.WriteLine("Press <ENTER>to terminate")

Console.ReadLine()

Host.Close()

Host2.Close()

}

catch (Exception e) { Console.WriteLine("Exception:" + e.Message.ToString())Console.ReadLine()}

}

请您关注BaseUri的变化

生成项目Exe后,请务必使用管理员身份运行!运行后在IE中输入

http://localhost:8002/Service1/

就可以看到Service1的服务概要了

替换成Service2后也可看到。

-------------------------

新手作答 多多包涵

可以有很多,不过WCF一般都是单独托管的。

在web.config中可以这样配置

<services>

<service

name="MyNamespace.DBService"

>

<host>

<baseAddresses>

<add

baseAddress="http://localhost:8001/"/>

</baseAddresses>

</host>

<endpoint

address="http://localhost:8001/DBService"

binding="wsHttpBinding"

contract="MyNamespace.IDBService">

</endpoint>

</service>

<service

name="MyNamespace.MyService">

<host>

<baseAddress>

<add

baseAddress="http://localhost:8001/"/>

<!--注意这里的基地址-->

</baseAddress>

</host>

<endpoint

address="http://localhost:8001/MyService"

binding="wsHttpBinding"

contract="MyNamespace.IMyService">

</endpoint>

</service>

</services>

这里的两个服务使用了相同的基地址,但URI不同。MyNamespace是你项目的命名空间,根据你的项目自己修改。

其实你也可以配置不同的基地址,例如:

<services>

<service

name="MyNamespace.DBService"

>

<host>

<baseAddresses>

<add

baseAddress="http://localhost:8001/"/>

</baseAddresses>

</host>

<endpoint

address="http://localhost:8001/DBService"

binding="wsHttpBinding"

contract="MyNamespace.IDBService">

</endpoint>

</service>

<service

name="MyNamespace.MyService">

<host>

<baseAddress>

<add

baseAddress="http://localhost:8002/"/>

<!--注意这里的基地址-->

</baseAddress>

</host>

<endpoint

address="http://localhost:8002/MyService"

binding="wsHttpBinding"

contract="MyNamespace.IMyService">

</endpoint>

</service>

</services>

还可以选择不同的绑定类型:

<services>

<service

name="MyNamespace.DBService"

>

<host>

<baseAddresses>

<add

baseAddress="http://localhost:8001/"/>

</baseAddresses>

</host>

<endpoint

address="http://localhost:8001/DBService"

binding="wsHttpBinding"

contract="MyNamespace.IDBService">

</endpoint>

</service>

<service

name="MyNamespace.MyService">

<host>

<baseAddress>

<add

baseAddress="net.tcp://localhost:8002/"/>

<!--注意这里的基地址-->

</baseAddress>

</host>

<endpoint

address="net.tcp://localhost:8002/MyService"

binding="netTcpBinding"

<!--使用TCP绑定-->

contract="MyNamespace.IMyService">

</endpoint>

</service>

</services>

关于WCF的配置还有很多种情况,比如还可以为同一个服务配置不同的基地址,或者为同一个服务配置不同的绑定类型。这些就不多说了,你最好买一本关于WCF的书系统的学习一下。WCF是下一代编程技术,必将替代面向组建技术,现在学习也算是先行者了。祝你好运!

补充一下:我这里写得配置都不是用于IIS/WAS托管的,而是需要直接与宿主实例交互。.svc文件是用于IIS/WAS托管的,可以不需要手动配置,直接在IIS/WAS上发布就行了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11643490.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存