gpt4 book ai didi

c# - 类型初始化异常 : The type initializer for 'vService.CheckService' threw an exception

转载 作者:太空狗 更新时间:2023-10-29 23:25:21 29 4
gpt4 key购买 nike

我在调用 WCF 服务时收到如下 FaultException:

2012-04-02 16:26:00.3593|Error|System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: The type initializer for 'vService.CheckService' threw an exception. (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.TypeInitializationException: The type initializer for 'vService.CheckService' threw an exception. ----> System.NullReferenceException: Object reference not set to an instance of an object.
at vService.CheckService..cctor() in d:\working\code\VioletServer\vService\CheckService.cs:line 14
--- End of inner ExceptionDetail stack trace ---
at vService.CheckService..ctor()
at CreatevService.CheckService()
at System.ServiceModel.Dispatcher.InstanceProvider.GetInstance(InstanceContext instanceContext, Message message)
at System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance(InstanceContext instanceContext, Message request)
at System.ServiceModel.InstanceContext.GetServiceInstance(Message message)
at System.ServiceModel.Dispatcher.InstanceBehavior.EnsureServiceInstance(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
...).

CheckService.cs的第14行是

private static string connStr = ConfigurationManager.ConnectionStrings["violetdb"].ConnectionString;

异常在这种情况下意味着什么,我该如何解决?


//CheckService.cs
public class CheckService : ICheckService
{
private static string connStr = ConfigurationManager.ConnectionStrings["violetdb"].ConnectionString;
MessageRepository _repo = new MessageRepository(connStr);

public CheckService(){}

public CheckService(MessageRepository repo)
{
_repo = repo;
}

public void SendMessage(string sender, string recipient, string messagetext)
{
_repo.DeliverMessage(sender,recipient,messagetext);
}
}

最佳答案

您的 WCF 服务很可能在其应用程序配置文件中没有名为“violetdb”的连接字符串。

A TypeInitializerException在这种情况下被抛出,因为你有一个 static在类型构造之前初始化的字段,因为 ConnectionStrings["violetdb"]电话正在返回 null , 它正在抛出 NullReferenceException .

底线,检查 <connectionStrings>配置文件的一部分并确保连接字符串存在。

关于c# - 类型初始化异常 : The type initializer for 'vService.CheckService' threw an exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9975404/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com