实体框架通过WCF服务(不含IIS) [英] Entity Framework through WCF service (without IIS)
问题描述
关于我之前打开的线程:
我有我的 WCF
服务中的一个简单的界面,其中一个方法获取一个 myRequest
参数
public interface IService
{
[OperationContract]
string MyOperation(myRequest request);
}
当我从客户端发布数据时,内容类型为 application / json
,所以请求身体自动反序列入 myRequest
对象。
myRequest
是一个 WCF
DataContract
:
[DataContract]
public class myRequest
{
string id;
string Name;
[DataMember]
public string Name {get;组; }
[DataMember]
public string Id {get;组;
public string MyOperation(myRequest request)
{
if(request!= null){
Contact contact = new联系();
contact.Id = Int32.Parse(request.Id);
contact.DisplayName = request.Name;
ContentContext db = new ContentContext();
db.Contacts.Add(contact);
db.SaveChanges();
returnok;
}
SetResponseHttpStatus(HttpStatusCode.InternalServerError);
返回null;
}
而且,托管服务:
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
它是程序
类的静态成员。
当我尝试使用EF通过 WCF休息
由Console / Application托管的服务。
当我尝试这样做 - 客户端总是得到 400错误请求
响应。
另一件事 - 我在数据库模型的解决方案中创建了一个新项目, WCF
服务是另一个项目,所以我将数据库模型项目的引用添加到服务项目中。要获得这个,我不得不安装并添加对Nuget Entityframe工作包的引用。
任何解决方案?
好的,所以我解决了这个问题。
已经在服务项目中安装了 NuGet
Entity框架库(我在数据库项目中使用了内置库)。
在数据库项目+上安装了 NuGet
Entity框架后,将 connectionString
添加到服务 App.config
一切似乎都可以。
Regarding this thread I've opened earlier:
Storing WCF rest request data with SQL Server stored procedure
I have a simple interface in my WCF
service with one method which gets a myRequest
parameter
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.
myRequest
is a WCF
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.
When I try to use the EF through WCF rest
service hosted by Console/Application.
When I'm trying to do it - the client gets always 400 Bad Request
response.
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. To achive this I had to install and add a refrence to the Nuget Entityframe work package.
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).
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.
这篇关于实体框架通过WCF服务(不含IIS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!