Joe*_*jr2 5 c# wcf entity-framework connection-string
请原谅这个极端新手的问题.我无法在其他任何地方找到答案.
我正在使用Entity Framework和WCF编写解决方案.我将写几个不同的客户.我已将数据库连接字符串放在Entity Framework项目的app.config和WCF主机的app.config文件中.
在宿主类中,我用一种方法编写了一个简单的测试服务契约,该方法只是从数据库(来自EF)检索所有客户,称为GetAllCustomers().然后我编写了一个简单的控制台客户端应用程序来调用主机的GetAllCustomers()并将所有客户的名字和姓氏写入控制台.
当我尝试运行客户端时,我收到一条错误消息,"在应用程序配置文件中找不到名为'TRS11Entities'的连接字符串.
如果我将连接字符串复制到客户端控制台应用程序的app.config文件,它可以正常工作,但如果我将其注释掉,它将无法再次运行.
由于WCF主机正在与数据库进行通信,而不是直接与客户端通信,因此我不明白为什么客户端需要连接字符串.我只能猜测连接字符串是从客户端传递到WCF主机到实体框架的链.如何让WCF主机告诉.NET,"降压停在这里!使用我的app.config的连接字符串并停止打扰客户端获取此信息!"
如果重要的话,我正在使用Visual Studio Pro 2012,所有项目都是用C#编写的,目标是V4.5 .NET框架.
来自WCF服务项目中的APP.CONFIG:
<system.serviceModel>
<services>
<service name="OrsonServiceLibrary.Service1">
<endpoint address="localhost" binding="basicHttpBinding" name="TRS11Entities"
contract="OrsonServiceLibrary.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/OrsonServiceLibrary/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<connectionStrings>
<add name="TRS11Entities" connectionString="metadata=res://*/TRS11Model.csdl|res://*/TRS11Model.ssdl|res://*/TRS11Model.msl;provider=FirebirdSql.Data.FirebirdClient;provider connection string='character set=NONE;data source=localhost;initial catalog="C:\Program Files (x86)\TRS11\Data\DATA1100.FDB";user id=sysdba;password=masterkey'" providerName="System.Data.EntityClient" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)
来自Entity Framework项目中的APP.CONFIG:
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="TRS11Entities" connectionString="metadata=res://*/TRS11Model.csdl|res://*/TRS11Model.ssdl|res://*/TRS11Model.msl;provider=FirebirdSql.Data.FirebirdClient;provider connection string='initial catalog="C:\Program Files (x86)\TRS11\Data\DATA1100.FDB";user id=sysdba;password=masterkey;data source=localhost'" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
Run Code Online (Sandbox Code Playgroud)
来自控制台客户端应用程序中的APP.CONFIG:
<system.serviceModel>
<client>
<endpoint address="http://localhost:8000/Service1" contract="OrsonServiceLibrary.IService1" binding="basicHttpBinding" />
</client>
</system.serviceModel>
<!--<connectionStrings>
<add name="TRS11Entities" connectionString="metadata=res://*/TRS11Model.csdl|res://*/TRS11Model.ssdl|res://*/TRS11Model.msl;provider=FirebirdSql.Data.FirebirdClient;provider connection string='character set=NONE;data source=localhost;initial catalog="C:\Program Files (x86)\TRS11\Data\DATA1100.FDB";user id=sysdba;password=masterkey'" providerName="System.Data.EntityClient" />
</connectionStrings>-->
Run Code Online (Sandbox Code Playgroud)
WCF服务项目中的GetAllCustomers()函数代码:
public HashSet<CUSTOMER> GetAllCustomers()
{
var db = new TRS11Entities();
HashSet<CUSTOMER> TheCusts = new HashSet<CUSTOMER>();
foreach (CUSTOMER c in db.CUSTOMERs)
{
TheCusts.Add(c);
}
return TheCusts;
}
Run Code Online (Sandbox Code Playgroud)
Console客户端应用代码:
static void Main(string[] args)
{
Console.WriteLine("Press Enter to begin.");
Console.ReadLine();
Service1 MyService = new Service1();
HashSet<CUSTOMER> cl = MyService.GetAllCustomers();
foreach (CUSTOMER c in cl)
{
Console.WriteLine(c.CUSTFNAME + " " + c.CUSTLNAME);
}
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
主机应用代码:
class Program
{
static void Main(string[] args)
{
ServiceHost hostA = null;
try
{
hostA = new ServiceHost(typeof(Service1));
hostA.Open();
Console.WriteLine();
Console.WriteLine("Host started. Press Enter to terminate host.");
Console.ReadLine();
}
finally
{
if (hostA.State == CommunicationState.Faulted)
hostA.Abort();
else
hostA.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢,乔
小智 2
有些事情告诉我,您实际上是在客户端应用程序中创建服务实例,而不是使用代理/通道。
客户端应用程序代码中的 Service1 类型是什么?是 OrsonServiceLibrary.Service1 吗?
Service1 MyService = new Service1();
Run Code Online (Sandbox Code Playgroud)
如果是这样,只需删除对服务项目的引用并添加服务引用即可。这将生成一个代理,您可以使用它来访问您的服务。
| 归档时间: |
|
| 查看次数: |
2290 次 |
| 最近记录: |