简体   繁体   English

找不到连接字符串

[英]Unable to find connection string

I have a WCF library project that is using the connection string referenced in WCF console app which is used as a host. 我有一个WCF库项目,该项目正在使用WCF控制台应用程序中引用的连接字符串作为主机。 Connection string is in App.Config file. 连接字符串在App.Config文件中。 But when I run a project I get an error in the library that it is unable to find connection string. 但是,当我运行一个项目时,我在库中收到一个错误,它找不到连接字符串。 I'm new to WCF so I'm not really sure how WCF host works. 我是WCF的新手,所以我不太确定WCF主机的工作方式。 Do you have any suggestions on how to make it work? 您对如何使其运作有任何建议吗?

This is the line of code in library that gets the error: 这是库中出现错误的代码行:

using (OrganizationService service = new 
OrganizationService("MyConnectionString"))

And this is the console app that is used to host a service: 这是用于托管服务的控制台应用程序:

static void Main(string[] args)
{
    ServiceHost selfHost = new ServiceHost(typeof(Service1));

    try
    {
        selfHost.AddServiceEndpoint(typeof(IService1), new 
            WSHttpBinding(), "Service1");

        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        selfHost.Description.Behaviors.Add(smb);

        selfHost.Open();
        Console.WriteLine("The service is ready.");

        Console.WriteLine("Press <Enter> to terminate the service.");
        Console.WriteLine();
        Console.ReadLine();
        selfHost.Close();
    }
    catch (CommunicationException ce)
    {
        Console.WriteLine("An exception occurred: {0}", ce.Message);
        selfHost.Abort();
    }
}

app.config: app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup> 
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
</startup>
<connectionStrings>
<add name="MyConnectionString" connectionString="
Server=https://crmtest.com:444;
Username=user;Password=pass"/>
</connectionStrings>
</configuration>

connection works when i try it with my other app 当我尝试使用其他应用程序时,连接有效

Im not sure why are you adding Service1. 我不确定为什么要添加Service1。 in your connection string name you just need to add the exact connection string name in app config 在您的连接字符串名称中,您只需要在应用程序配置中添加确切的连接字符串名称

so it should just be 所以应该是

<connectionStrings>
<add name="MyConnectionString" connectionString="
Server=https://crmtest.com:444;Initial Catalog="YourDatabaseName";
Username=user;Password=pass"/>
</connectionStrings>

I think there might be something wrong with the configuration file. 我认为配置文件可能有问题。 Ordinarily, when we use WCF library project to create the WCF service, we should move our configuration code into the configuration file recognized by your hosting environment. 通常,当我们使用WCF库项目创建WCF服务时,应将我们的配置代码移入您的托管环境可以识别的配置文件中。 Namely, the Appconfig file of the library project does not take effects, we should copy the System.servicemodel section to the actual project. 即,库项目的Appconfig文件不会生效,我们应该将System.servicemodel部分复制到实际项目中。
Please refer to the below link. 请参考以下链接。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/deploying-a-wcf-library-project https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/deploying-a-wcf-library-project
Feel free to let me know if the problem still exists. 随时让我知道问题是否仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM