![](/img/trans.png)
[英]WCF Service, the type provided as the service attribute values…could not be found
[英]WCF Singleton-Service error: The service type provided could not be loaded as a service because it does not have a default constructor
我在WCF中遇到以下奇怪的问题,无法确定原因:
我正在与WCF合作,以确定是否将其用于需要为类似打印机的设备实现的远程控制API。 该设备由Windows-PC控制,该Windows-PC运行.Net中实现的控制器软件。 我需要为该软件实现API。
该服务是从控制器软件内部自我托管的,目前我正在弄清楚如何创建WCF服务的单例实例,以便可以通过控制器软件来创建具有相应对象/类的实例。 我已经使用简化版本使它工作了,但是奇怪的是,如果服务不包含默认(无参数)构造函数,则会收到此警告。 即使是很奇怪,我也确实在做,第二句话告诉我的异常(或者至少我想以为是)。 此异常在带有标题WCF Service Host
单独窗口中引发,此后此程序继续正常执行:
System.InvalidOperationException:由于提供的服务类型没有默认(无参数)构造函数,因此无法作为服务加载。 要解决此问题,请向类型添加默认构造函数,或将类型的实例传递给主机。
在System.ServiceModel.Description.ServiceDescription.CreateImplementation(Type serviceType)
在System.ServiceModel.Description.ServiceDescription.SetupSingleton(ServiceDescription serviceDescription,对象实现,布尔值isWellKnown)
在System.ServiceModel.Description.ServiceDescription.GetService(类型serviceType)
在System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2&ImplementedContracts)
在System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses)
在System.ServiceModel.ServiceHost..ctor(类型serviceType,Uri [] baseAddresses)
在Microsoft.Tools.SvcHost.ServiceHostHelper.CreateServiceHost(类型,ServiceKind类型)
在Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo信息)
这是我用来创建服务的代码。 我在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();
}
}
Service.cs
:
[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();
}
}
IServices.cs
:
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服务主机...”选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.