【问题标题】:Why does throws me a FaultException in WCF Service?为什么在 WCF 服务中向我抛出 FaultException?
【发布时间】:2013-01-26 19:42:20
【问题描述】:

让我澄清一下我的情况。我有一个使用库的 WCF 服务,在这个库中存在数据库模型 (edmx)。

在我的 WCF 服务中:

[DataContract]
public class QuestionSetInformation
{
    [DataMember]
    public string Id { get; set; }
    [DataMember]
    public string SetName { get; set; }
    [DataMember]
    public string ObjectiveName { get; set; }
}

[ServiceContract]
public interface IService1
{
    [OperationContract]
    QuestionSetInformation[] GetQuestionSets(string objectiveName);
}

public QuestionSetInformation[] GetQuestionSets(string objectiveName)
{
    var query = from r in QuestionRepositoryManager.GetRepositories()
                select new QuestionSetInformation()
                {
                   Id = r.Id,
                   SetName = r.SetName,
                   ObjectiveName = r.ObjectiveName
                };

    return query.ToArray();
}

由于我的库使用 EDMX,所以我将连接字符串移到了我的 WCF,它使用 WCF 测试客户端完美运行。它满足了我的期望。

<?xml version="1.0"?>
<configuration>
  <configSections>
    <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="ContosoDb" connectionString="metadata=res://*/Entities.Model1.csdl|res://*/Entities.Model1.ssdl|res://*/Entities.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=X;initial catalog=contoso2_db;persist security info=True;user id=X;password=X;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>

但在桌面应用程序中使用此 WCF 时,我遇到了以下异常:

        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client("BasicHttpsBinding_IService1");
        ServiceReference1.QuestionSetInformation[] qss = service.GetQuestionSets("something");

FaultException`1 未处理 使用 T4 模板生成的代码 对于 Database First 和 Model First 开发可能无法正常工作 如果在 Code First 模式下使用。继续使用 Database First 或 Model 首先确保指定了实体框架连接字符串 在执行应用程序的配置文件中。要使用这些类, 从 Database First 或 Model First 生成的,带有代码 首先使用属性或 DbModelBuilder API,然后删除引发此问题的代码 例外。

我在网上调查了这个错误,每个人都说使用连接字符串有问题,但我认为情况不一样。我不知道如何解决它

【问题讨论】:

  • 也许你对这个异常的来源感到困惑。它来自 服务器 而不是客户端。您只能在客户端捕获它。
  • 但我不明白为什么只有在 WCF 投入生产时才抛出这个错误

标签: c# wcf entity-framework entity-framework-4.1 entity-framework-5


【解决方案1】:

这是它使用的连接字符串的问题...它使用某种默认的 SqlClient 连接字符串(即不是指定三个 EF 元数据文件的实体框架连接字符串),使其认为您正在代码中运行第一种模式。

确保您的连接字符串名称相同,或者您将要使用的连接字符串名称传递给 DbContext 构造函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2011-08-29
    • 1970-01-01
    • 2010-11-02
    相关资源
    最近更新 更多