我同意 wcf 工具的文档缺乏,这很令人难过,因为它是一个非常出色的工具,如果人们因为无法开始使用而没有使用它,那将是一个真正的耻辱,所以让我看看我是否可以提供帮助如果可以的话,你出去一下……
让我们创建一个包含三个项目的应用程序:
- 共享合约的类库
- 充当服务器的控制台应用程序
- 充当客户端的控制台应用程序
我们的想法是,我们希望在注册服务时能够使用服务名称并共享基本 URL(我认为这就是您所要求的,如果没有,希望您可以从这里推断)。因此,首先,共享合约中只包含以下内容(没什么特别的,正常的 WCF 费用):
[ServiceContract]
public interface IMyService1
{
[OperationContract]
void DoSomething();
}
[ServiceContract]
public interface IMyService2
{
[OperationContract]
void DoSomethingToo();
}
现在服务器控制台应用程序看起来像这样,我们首先实现服务契约(同样没有什么特别的,只是实现接口的类),然后将它们全部注册为服务(注意这里不需要任何配置文件,您可以更改您的方式)使用温莎为您提供的所有选项来决定什么是服务等 - 我的方案有点有限,但它给了您一个想法):
namespace Services
{
public class MyService1 : IMyService1
{
public void DoSomething()
{
}
}
public class MyService2 : IMyService2
{
public void DoSomethingToo()
{
}
}
}
//... In some other namespace...
class Program
{
// Console application main
static void Main()
{
// Construct the container, add the facility and then register all
// the types in the same namespace as the MyService1 implementation
// as WCF services using the name as the URL (so for example
// MyService1 would be http://localhost/MyServices/MyService1) and
// with the default interface as teh service contract
var container = new WindsorContainer();
container.AddFacility<WcfFacility>(
f => f.CloseTimeout = TimeSpan.Zero);
container
.Register(
AllTypes
.FromThisAssembly()
.InSameNamespaceAs<MyService1>()
.WithServiceDefaultInterfaces()
.Configure(c =>
c.Named(c.Implementation.Name)
.AsWcfService(
new DefaultServiceModel()
.AddEndpoints(WcfEndpoint
.BoundTo(new WSHttpBinding())
.At(string.Format(
"http://localhost/MyServices/{0}",
c.Implementation.Name)
)))));
// Now just wait for a Q before shutting down
while (Console.ReadKey().Key != ConsoleKey.Q)
{
}
}
}
那就是服务器,现在如何消费这些服务呢?嗯,实际上这很简单,这是一个客户端控制台应用程序(它仅引用合约类库):
class Program
{
static void Main()
{
// Create the container, add the facilty and then use all the
// interfaces in the same namespace as IMyService1 in the assembly
// that contains the aforementioned namesapce as WCF client proxies
IWindsorContainer container = new WindsorContainer();
container.AddFacility<WcfFacility>(
f => f.CloseTimeout = TimeSpan.Zero);
container
.Register(
Types
.FromAssemblyContaining<IMyService1>()
.InSameNamespaceAs<IMyService1>()
.Configure(
c => c.Named(c.Implementation.Name)
.AsWcfClient(new DefaultClientModel
{
Endpoint = WcfEndpoint
.BoundTo(new WSHttpBinding())
.At(string.Format(
"http://localhost/MyServices/{0}",
c.Name.Substring(1)))
})));
// Now we just resolve them from the container and call an operation
// to test it - of course, now they are in the container you can get
// hold of them just like any other Castle registered component
var service1 = container.Resolve<IMyService1>();
service1.DoSomething();
var service2 = container.Resolve<IMyService2>();
service2.DoSomethingToo();
}
}
就是这样 - 希望这能让你开始(我发现实验和使用智能感知通常能让我到达我需要去的地方)。我向您展示了服务端和客户端,但如果您愿意,可以只使用其中之一。
您应该能够看到绑定的配置位置以及我如何构建 URL,因此在您的情况下,您可以轻松地从配置文件或任何您想要执行的操作中提取基本 URL。
最后要提到的是,您可以通过将自定义端点行为添加为端点的扩展来添加自定义端点行为,因此在客户端示例中,您将具有如下所示的内容:
Endpoint = WcfEndpoint
.BoundTo(new WSHttpBinding())
.At(string.Format("http://localhost/MyServices/{0}", c.Name.Substring(1)))
.AddExtensions(new AuthTokenBehavior())