为什么在WCF服务中向我抛出FaultException? [英] Why does throws me a FaultException in WCF Service?

查看:112
本文介绍了为什么在WCF服务中向我抛出FaultException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let me clarify my scenario. I've got a WCF Service where uses a library, in this library exist the database model (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 Test Client可以正常运行.它满足了我的期望.

As my library uses the EDMX, so I moved the connection strings to my WCF and it works PERFECTLY using the WCF Test Client. It delivers me what I'm expecting.

<?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时,出现以下异常:

But at a time using this WCF in a desktop application, I've got the following exception:

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

FaultException`1未使用T4模板生成的代码 for Database First和Model First开发可能无法正常工作 如果在代码优先"模式下使用.继续使用数据库优先或模型 首先确保指定了Entity Framework连接字符串 在执行应用程序的配置文件中.要使用这些类, 从数据库优先或模型优先使用代码生成的 首先使用属性或添加任何其他配置 DbModelBuilder API,然后删除引发此错误的代码

FaultException`1 was unhandled Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

我已经在网络上调查了此错误,每个人都使用连接字符串讲述了一个问题,但我认为情况不尽相同.我不知道如何解决

I have investigated this error in the web, everyone tells about a problem using the connection string but I supposed it's not the same case. I don't have idea about how to fix it

推荐答案

这是它正在使用的连接字符串的问题...它使用了某种默认的SqlClient连接字符串(即不是指定了三个EF元数据文件),使其认为您正在代码优先"模式下运行.

This is a problem with the connection string it's using... It's using some sort of default SqlClient connection string (i.e. not an entity framework connections string that specifies the three EF metadata files) making it think you're running in Code First mode.

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

Be sure that your connection string is named the same or that you pass the connection string name you want to use into your DbContext constructor.

这篇关于为什么在WCF服务中向我抛出FaultException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆