You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prism模块中托管WCF服务无响应问题求助

Can I create a ServiceHost in a class library-type Prism module?

Absolutely, you can create a ServiceHost within a class library-based Prism module—there's no hard technical prohibition here. The issues you're facing are almost certainly tied to configuration context, lifecycle management, or Prism's container/module loading behavior, not a fundamental restriction. Let's break down the key pitfalls and solutions:

1. Configuration Context Mismatch

Class libraries don't have their own runtime configuration file—they inherit the config of the hosting EXE. If you added WCF settings (like service behaviors, bindings, addresses) to your module's app.config, those won't be loaded at runtime.

Fix:

  • Move all WCF service configuration (including the serviceBehaviors with httpGetEnabled/httpGetUrl) into the hosting EXE's App.config.
  • Verify that the service name, contract, and address in the EXE's config exactly match what your module's ServiceHost is using.

2. ServiceHost Lifecycle & Garbage Collection

If your ServiceHost instance isn't properly retained, it might be garbage collected after the module initializes—even if it showed an "Opened" state.

Fix:

  • Ensure the ServiceHost is held by a long-lived object (e.g., register it as a singleton in Prism's container, or store it in a static field in your module class).
  • Delay calling host.Open() until after the host application is fully initialized. For Prism, you can override OnInitialized in your module, or hook into the Application.Current.Startup event to start the service later.

3. Socket Permissions & Listening Validation

Even if ServiceHost.Open() doesn't throw, the underlying socket might not be listening correctly due to permissions or port conflicts.

Check:

  • Run netstat -ano in Command Prompt to confirm the port specified in your netTcpBinding is actually being listened to by your hosting EXE's process ID.
  • Ensure the hosting EXE has sufficient permissions to listen on the netTcp port (for non-admin users, you might need to reserve the port using netsh http add urlacl).

4. WCF Diagnostic Log Deep Dive

You mentioned adding diagnostics—don't just check for errors, look for details about incoming requests:

  • Does the log show the client request reaching the server?
  • Is there a failure to instantiate your MyService class (e.g., missing dependencies in the constructor)?
  • For the blank WSDL page: Confirm the httpGetUrl matches the address you're accessing, and that the service contract is properly annotated with [ServiceContract] and [OperationContract] attributes.

5. Prism Container & Service Instance Creation

If your MyService class has dependencies injected via Prism, WCF's default instance provider won't know how to resolve them. This can lead to silent failures where the service instance isn't created, so your method breakpoints never trigger.

Fix:
Implement a custom IInstanceProvider that uses Prism's container to resolve service instances:

public class PrismInstanceProvider : IInstanceProvider
{
    private readonly IContainerExtension _container;
    private readonly Type _serviceType;

    public PrismInstanceProvider(IContainerExtension container, Type serviceType)
    {
        _container = container;
        _serviceType = serviceType;
    }

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return _container.Resolve(_serviceType);
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return GetInstance(instanceContext, null);
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {
        // Let Prism handle disposal if needed
    }
}

Then attach it to your ServiceHost:

public ServiceHost Host { get; private set; }

public void Initialize(IModuleInitializer moduleInitializer)
{
    var container = moduleInitializer.ModuleManager.ModuleCatalog.Container;
    Host = new ServiceHost(typeof(MyService));
    var serviceBehavior = Host.Description.Behaviors.Find<ServiceBehaviorAttribute>() 
                          ?? new ServiceBehaviorAttribute();
    serviceBehavior.InstanceProvider = new PrismInstanceProvider(container, typeof(MyService));
    Host.Description.Behaviors.Add(serviceBehavior);

    try
    {
        Host.Open();
    }
    catch (Exception ex)
    {
        // Log error
        throw;
    }
}

Summary

There's no taboo against hosting WCF services in a Prism class library module. The most likely fixes for your scenario are:

  1. Moving WCF config to the hosting EXE's App.config
  2. Ensuring the ServiceHost is not garbage collected
  3. Resolving dependency injection conflicts for your service class

内容的提问来源于stack exchange,提问作者profou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:42:05