Prism模块中托管WCF服务无响应问题求助
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
serviceBehaviorswithhttpGetEnabled/httpGetUrl) into the hosting EXE'sApp.config. - Verify that the service name, contract, and address in the EXE's config exactly match what your module's
ServiceHostis 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
ServiceHostis 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 overrideOnInitializedin your module, or hook into theApplication.Current.Startupevent 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 -anoin 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
MyServiceclass (e.g., missing dependencies in the constructor)? - For the blank WSDL page: Confirm the
httpGetUrlmatches 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:
- Moving WCF config to the hosting EXE's
App.config - Ensuring the
ServiceHostis not garbage collected - Resolving dependency injection conflicts for your service class
内容的提问来源于stack exchange,提问作者profou

