简体   繁体   English

通过WCF服务的实体框架(无IIS)

[英]Entity Framework through WCF service (without IIS)

Regarding this thread I've opened earlier: 关于这个线程,我之前已经打开了:

Storing WCF rest request data with SQL Server stored procedure 使用SQL Server存储过程存储WCF休息请求数据

I have a simple interface in my WCF service with one method which gets a myRequest parameter 我的WCF服务中有一个简单的接口,其中一个方法获得了myRequest参数

public interface IService
{
    [OperationContract]
    string MyOperation(myRequest request);
}

When I'm posting the data from the client, the content type is application/json , so the request body auto deserialise into myRequest object. 当我从客户端发布数据时,内容类型为application/json ,因此请求主体自动反序列化为myRequest对象。

myRequest is a WCF DataContract : myRequestWCF DataContract

[DataContract]
public class myRequest

{
    string id;
    string Name;

    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Id { get; set; }

}

public string MyOperation(myRequest request)
{
    if (request != null) {
        Contact contact = new Contact();
        contact.Id = Int32.Parse(request.Id);
        contact.DisplayName = request.Name;

        ContentContext db = new ContentContext();
        db.Contacts.Add(contact);
        db.SaveChanges();

        return "ok";
    }

    SetResponseHttpStatus(HttpStatusCode.InternalServerError);
    return null;
}

And finaly, hosting the service: 最后,托管服务:

public class ProgramBL {
    public static void StartServer()
        {

            ServiceHost streamer = new ServiceHost(typeof(DataStreaming.StreamService));


            streamer.Open();
            Console.WriteLine("Service up and running at:");
            foreach (var ea in streamer.Description.Endpoints)
            {
                Console.WriteLine(ea.Address);
            }

            Program._StreamerHost = streamer;
        }
}

_StreamerHost its a static member of the Program class. _StreamerHostProgram类的静态成员。

When I try to use the EF through WCF rest service hosted by Console/Application. 当我尝试通过控制台/应用程序托管的WCF rest服务使用EF时。 When I'm trying to do it - the client gets always 400 Bad Request response. 当我尝试执行此操作时,客户端始终会收到400 Bad Request响应。

Another thing - I created a new project in the solution for the database model, the WCF service is another project, so I added refrence of the database model project to the service project. 另一件事-我在数据库模型的解决方案中创建了一个新项目, WCF服务是另一个项目,因此我在服务项目中添加了数据库模型项目的引用。 To achive this I had to install and add a refrence to the Nuget Entityframe work package. 为此,我必须安装Nufr Entityframe工作包并将其添加到其中。

Any solutions? 有什么办法吗?

Thanks 谢谢

Ok so I solved the problem. 好的,所以我解决了这个问题。

I have installed the NuGet Entity framework library only at the service project (I used the built in library at the database project). 我仅在服务项目中安装了NuGet Entity框架库(我在数据库项目中使用了内置库)。 After I have installed the NuGet Entity framework on the database project + added the connectionString to the service App.config everything seems to be OK. 在数据库项目上安装NuGet Entity框架+将connectionString添加到服务App.config一切似乎都正常。

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

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