代码之家  ›  专栏  ›  技术社区  ›  Merlyn Morgan-Graham

如何提供类的特定实例,以作为WCF服务公开

wcf
  •  2
  • Merlyn Morgan-Graham  · 技术社区  · 14 年前

    有没有办法将现有的类实例传递给WCF服务主机,以作为服务端点公开?

    我知道(或能想出)如何创建WCF服务的单例实例,但这仍然帮不了我。据我所知,单例实例仍将由WCF创建和提供。

    我也想过其他的方法,但如果可以的话,我宁愿选择这个方法。

    一些密码。这在我的插件的构造函数中:

    // Setup the service host
    var baseAddress = new Uri("http://localhost:8080/MyService/");
    this.serviceHost = new ServiceHost(this.GetType(), baseAddress);
    
    // Add our service endpoint
    // Todo: Is there somewhere around here that I can provide an instance?
    //   Maybe in behavior somewhere?
    this.serviceHost.AddServiceEndpoint(
        typeof(ITheInterfaceMyClassDerivesFrom),
        new BasicHttpBinding(),
        ""
        );
    
    // Add metadata exchange (so we see something when we go to that URL)
    var serviceMetadataBehavior = this.serviceHost.Description.Behaviors
        .Find<ServiceMetadataBehavior>();
    if (serviceMetadataBehavior == null)
        this.serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
    
    this.serviceHost.AddServiceEndpoint(
        typeof(IMetadataExchange),
        new CustomBinding(new HttpTransportBindingElement()),
        "MEX"
        );
    

    serviceHost.Open();
    
    1 回复  |  直到 14 年前
        1
  •  6
  •   marc_s    14 年前

    您需要使用另一个构造函数 ServiceHost 如果你想这样做-查看MSDN文档 http://msdn.microsoft.com/en-us/library/ms585487.aspx

    public ServiceHost(
        Object singletonInstance,
        params Uri[] baseAddresses
    )