简体   繁体   English

使用MVC4实体框架实现WCF调用方法失败

[英]Implementing wcf with mvc4 entity framework fails invoking method

I have a wcf application in which i have used Entitity Framework and have implemented dbContext for querying the database. 我有一个wcf应用程序,在其中使用了Entitity Framework并实现了dbContext来查询数据库。 When I view the svc file in browser it exposes the operations. 当我在浏览器中查看svc文件时,它会显示操作。

I have interface class like this: 我有这样的接口类:

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
       List<BooksModels> GetBooksList();

        [OperationContract]
        BooksModels GetBook(int id);

    }

I have the implementation in the svc.cs file like this 我在svc.cs文件中有这样的实现

    public List<BooksModels> GetBooksList()
    {
        MVCEntity en = new MVCEntity();
      return  en.book.ToList();

    }

    public int GetBookId(int id)
    {
        //return db.book.Find(id);
        return 1;
    }

and the BooksModels class is like this 而BooksModels类是这样的

[DataContract]
    public class BooksModels
        {
            [Key]
            [DataMember]
            public int BookId { get; set; }
            [DataMember]
    public string BookName{get;set;}
    }

and have the config file the default one as created when creating wcf service application . 并使配置文件是创建wcf服务应用程序时创建的默认文件。

but when i invoke GetBooksList the service from MVC wcf client it gives me the following error: 但是,当我从MVC wcf客户端调用GetBooksList服务时,出现以下错误:

Failed to invoke the service. 调用服务失败。 Possible causes: The service is offline or inaccessible; 可能的原因:服务离线或无法访问; the client-side configuration does not match the proxy; 客户端配置与代理不匹配; the existing proxy is invalid. 现有代理无效。 Refer to the stack trace for more detail. 有关更多详细信息,请参考堆栈跟踪。 You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service. 您可以尝试通过启动新代理,恢复为默认配置或刷新服务来进行恢复。

but when i invoke the second method that returns 1. 但是当我调用返回1的第二个方法时。

i examined that when the service uses the dbContext to return data it gives error and is fine when not. 我检查了该服务使用dbContext返回数据时会给出错误,而在没有错误的情况下是可以的。

I have gone through various blogs and also the questions in stackoverflow but didn't help. 我浏览过各种博客以及stackoverflow中的问题,但没有帮助。 so how can this problem be addressed. 那么如何解决这个问题。 Thanks 谢谢

I think it's a problem of serialization. 我认为这是序列化的问题。 Make a serialisation test from you're business layer that take List from entity framework and serialize then deserialize it and compare before and after serialization. 从业务层进行序列化测试,该测试从实体框架获取List并进行序列化,然后反序列化并在序列化前后进行比较。

Use DataContractSerializer : 使用DataContractSerializer:

DataContractSerializer serialiser = new DataContractSerializer(typeof(List<BooksModels>));
List<BooksModels> expected = business.GetBooksList();
Stream stream = new MemoryStream();
serialiser.WriteObject(stream, expected);
stream.Position = 0;
List<BooksModels> actual = serialiser.ReadObject(stream) as List<BooksModels>;
Assert.IsNotNull(actual);
Assert.AreEqual(expected.Prop1, actual.Prop1);
Assert.AreEqual(expected.Prop2, actual.Prop2);
// ... //

If it doesn't work you probably use proxy in entity framework. 如果不起作用,则可能在实体框架中使用代理。 turn off proxy : 关闭代理:

context.ContextOptions.ProxyCreationEnabled = false;

I had the same problem. 我有同样的问题。 Thanks to Brice2Paris, I solved it by turning off the proxy of EF: 感谢Brice2Paris,我通过关闭EF代理解决了该问题:

context.Configuration.ProxyCreationEnabled = false;

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

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