C# WCF Singleton服务错误:无法将提供的服务类型作为服务加载,因为它没有默认构造函数
我对WCF有以下奇怪的问题,我无法找出原因: 我正在与WCF合作,以确定是否将其用于需要为类似打印机的设备实现的远程控制API。该设备由运行.Net中实现的控制器软件的Windows PC控制。我需要为这个软件实现API 该服务是控制器软件内部的自托管服务,我目前正在研究如何创建WCF服务的单例实例,以便使用控制器软件创建具有相应对象/类的实例。我已经使用简化版本实现了这一点,但奇怪的是,如果服务不包含默认(无参数)构造函数,我会得到这个警告。更奇怪的是,我正在做的正是第二句中例外告诉我的(或者至少我喜欢认为我是)。此异常在标题为“WCF服务主机”的单独窗口中引发,程序随后继续正常执行: System.InvalidOperationException:无法将提供的服务类型作为服务加载,因为它没有默认(无参数)构造函数。要解决此问题,请向该类型添加默认构造函数,或将该类型的实例传递给主机 位于System.ServiceModel.Description.ServiceDescription.CreateImplementation(类型serviceType) 位于System.ServiceModel.Description.ServiceDescription.SetupSingleton(ServiceDescription ServiceDescription,对象实现,布尔值是众所周知的) 位于System.ServiceModel.Description.ServiceDescription.GetService(类型serviceType) 位于System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2&implementedContracts) 位于System.ServiceModel.ServiceHostBase.InitializeDescription(UrischemeKeydCollection基地址) 位于System.ServiceModel.ServiceHost..ctor(类型serviceType,Uri[]baseAddresses) 在Microsoft.Tools.SvcHost.ServiceHostHelper.CreateServiceHost(类型,ServiceKind) 位于Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo信息) 下面是我用来创建服务的代码。我在C# WCF Singleton服务错误:无法将提供的服务类型作为服务加载,因为它没有默认构造函数,c#,.net,wcf,C#,.net,Wcf,我对WCF有以下奇怪的问题,我无法找出原因: 我正在与WCF合作,以确定是否将其用于需要为类似打印机的设备实现的远程控制API。该设备由运行.Net中实现的控制器软件的Windows PC控制。我需要为这个软件实现API 该服务是控制器软件内部的自托管服务,我目前正在研究如何创建WCF服务的单例实例,以便使用控制器软件创建具有相应对象/类的实例。我已经使用简化版本实现了这一点,但奇怪的是,如果服务不包含默认(无参数)构造函数,我会得到这个警告。更奇怪的是,我正在做的正是第二句中例外告诉我的(或者
Service.cs
中注释了包含默认构造函数的注释行。有趣的是,当我包含默认构造函数(因此永远不会抛出错误)时,它永远不会被调用(我通过设置断点确认了这一点)。如果您取消了注释,则不会引发异常
Server.cs
:
public class Server
{
private ServiceHost svh;
private Service service;
public Server()
{
service = new Service("A fixed ctor test value that the service should return.");
svh = new ServiceHost(service);
}
public void Open(string ipAdress, string port)
{
svh.AddServiceEndpoint(
typeof(IService),
new NetTcpBinding(),
"net.tcp://"+ ipAdress + ":" + port);
svh.Open();
}
public void Close()
{
svh.Close();
}
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant,
InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{
private string defaultString;
public Service(string ctorTestValue)
{
this.defaultString = ctorTestValue;
}
//// when this constructor is uncommented, I do not get the error
//public Service()
//{
// defaultString = "Default value from the ctor without argument.";
//}
public string GetDefaultString()
{
return defaultString;
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
public string Ping(string name)
{
Console.WriteLine("SERVER - Processing Ping('{0}')", name);
return "Hello, " + name;
}
static Action m_Event1 = delegate { };
static Action m_Event2 = delegate { };
public void SubscribeEvent1()
{
IMyEvents subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
m_Event1 += subscriber.Event1;
}
public void UnsubscribeEvent1()
{
IMyEvents subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
m_Event1 -= subscriber.Event1;
}
public void SubscribeEvent2()
{
IMyEvents subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
m_Event2 += subscriber.Event2;
}
public void UnsubscribeEvent2()
{
IMyEvents subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
m_Event2 -= subscriber.Event2;
}
public static void FireEvent1()
{
m_Event1();
}
public static void FireEvent2()
{
m_Event2();
}
public static Timer Timer1;
public static Timer Timer2;
public void OpenSession()
{
Timer1 = new Timer(1000);
Timer1.AutoReset = true;
Timer1.Enabled = true;
Timer1.Elapsed += OnTimer1Elapsed;
Timer2 = new Timer(500);
Timer2.AutoReset = true;
Timer2.Enabled = true;
Timer2.Elapsed += OnTimer2Elapsed;
}
void OnTimer1Elapsed(object sender, ElapsedEventArgs e)
{
FireEvent1();
}
void OnTimer2Elapsed(object sender, ElapsedEventArgs e)
{
FireEvent2();
}
}
public interface IMyEvents
{
[OperationContract(IsOneWay = true)]
void Event1();
[OperationContract(IsOneWay = true)]
void Event2();
}
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract(CallbackContract = typeof(IMyEvents))]
public interface IService
{
[OperationContract]
string GetData(int value);
[OperationContract]
string GetDefaultString();
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
[OperationContract]
string Ping(string name);
[OperationContract]
void SubscribeEvent1();
[OperationContract]
void UnsubscribeEvent1();
[OperationContract]
void SubscribeEvent2();
[OperationContract]
void UnsubscribeEvent2();
[OperationContract]
void OpenSession();
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
// You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WcfService.ContractType".
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
Service.cs
:
public class Server
{
private ServiceHost svh;
private Service service;
public Server()
{
service = new Service("A fixed ctor test value that the service should return.");
svh = new ServiceHost(service);
}
public void Open(string ipAdress, string port)
{
svh.AddServiceEndpoint(
typeof(IService),
new NetTcpBinding(),
"net.tcp://"+ ipAdress + ":" + port);
svh.Open();
}
public void Close()
{
svh.Close();
}
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant,
InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{
private string defaultString;
public Service(string ctorTestValue)
{
this.defaultString = ctorTestValue;
}
//// when this constructor is uncommented, I do not get the error
//public Service()
//{
// defaultString = "Default value from the ctor without argument.";
//}
public string GetDefaultString()
{
return defaultString;
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
public string Ping(string name)
{
Console.WriteLine("SERVER - Processing Ping('{0}')", name);
return "Hello, " + name;
}
static Action m_Event1 = delegate { };
static Action m_Event2 = delegate { };
public void SubscribeEvent1()
{
IMyEvents subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
m_Event1 += subscriber.Event1;
}
public void UnsubscribeEvent1()
{
IMyEvents subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
m_Event1 -= subscriber.Event1;
}
public void SubscribeEvent2()
{
IMyEvents subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
m_Event2 += subscriber.Event2;
}
public void UnsubscribeEvent2()
{
IMyEvents subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
m_Event2 -= subscriber.Event2;
}
public static void FireEvent1()
{
m_Event1();
}
public static void FireEvent2()
{
m_Event2();
}
public static Timer Timer1;
public static Timer Timer2;
public void OpenSession()
{
Timer1 = new Timer(1000);
Timer1.AutoReset = true;
Timer1.Enabled = true;
Timer1.Elapsed += OnTimer1Elapsed;
Timer2 = new Timer(500);
Timer2.AutoReset = true;
Timer2.Enabled = true;
Timer2.Elapsed += OnTimer2Elapsed;
}
void OnTimer1Elapsed(object sender, ElapsedEventArgs e)
{
FireEvent1();
}
void OnTimer2Elapsed(object sender, ElapsedEventArgs e)
{
FireEvent2();
}
}
public interface IMyEvents
{
[OperationContract(IsOneWay = true)]
void Event1();
[OperationContract(IsOneWay = true)]
void Event2();
}
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract(CallbackContract = typeof(IMyEvents))]
public interface IService
{
[OperationContract]
string GetData(int value);
[OperationContract]
string GetDefaultString();
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
[OperationContract]
string Ping(string name);
[OperationContract]
void SubscribeEvent1();
[OperationContract]
void UnsubscribeEvent1();
[OperationContract]
void SubscribeEvent2();
[OperationContract]
void UnsubscribeEvent2();
[OperationContract]
void OpenSession();
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
// You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WcfService.ContractType".
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
Main
用于启动服务器:
static void Main(string[] args)
{
// start server
var server = new Server();
server.Open("localhost", "6700");
Console.WriteLine("Server started.");
Console.ReadLine();
server.Close();
}
该问题是由在Visual Studio中调试时运行的WcfSvcHost引起的。根据“WCF服务主机枚举WCF服务项目中的服务,加载项目的配置,并为找到的每个服务实例化一个主机。该工具通过WCF服务模板集成到Visual Studio中,并在您开始调试项目时调用。”
您不需要使用WCF服务主机,因为您是自托管的,所以您可以通过包含该服务的项目的“项目属性”页面禁用它。您应该在属性页上看到“WCF选项”选项卡。在此情况下,请关闭“调试时启动WCF服务主机…”选项。我复制了您的代码并从我的环境中运行了它,但问题没有出现。WcfSvcHost在VS中调试时是否正在运行?请参阅。@KimJohnson在您的建议之后,我尝试从VS外部启动我的小测试程序(没有默认的无参数构造函数),但我没有得到上面描述的错误。所以我猜这可能与你的建议有关。然而,在阅读了你提供的链接后,我仍然不明白到底是什么问题。还有什么建议吗?顺便说一句:我忘了在我的问题中提到:通过设置断点,我确认,默认的ctor(当存在时)永远不会被调用。谢谢并致以最诚挚的问候。看起来它正在尝试承载您的代码的早期版本。清理您的环境,删除bin和obj文件夹。@CodeCaster感谢您的建议。我尝试删除bin和obj文件夹,并执行解决方案->清理->重建。删除bin文件夹时,我发现Windows告诉我无法删除它,因为它正在使用中。关闭VS后,我可以删除它。不幸的是,错误依然存在。还有其他建议吗?可能是因为某种原因,它启动了两个服务或其他什么?奇怪的是……这就成功了,当程序启动时,工具栏上的“恼人的”WcfHostService弹出窗口就消失了。非常感谢!