简体   繁体   English

消耗WCF数据服务EF,无法保存Change()

[英]Consuming WCF Data Service EF , Failed to SaveChange()

I am consuming WCF Data Service as Following: 我正在使用WCF数据服务,如下所示:

 DataMan.ContextWrapper context = new DataMan.ContextWrapper(new Uri("http://localhost:2060/PCM/DataMan.svc/rest/"));
            DataMan.Report newReport = DataMan.Report.CreateReport("123123123123", DateTime.Now, "999.199905171156550187000.25");

            newReport.Title = "tt";
            newReport.StudyAcDate = Convert.ToDateTime("2016-05-04 12:09:00");
            newReport.Body = "asdasd";
            newReport.Auther = "ali.h";
            newReport.ApproverComment = "cm";
            newReport.Approver = "admin";
            context.AddToReports(newReport);
            DataServiceResponse response = context.SaveChanges();

but after calling SaveChange() I have got the following error: 但是在调用SaveChange()之后,出现以下错误:

The server encountered an error processing the request. 服务器在处理请求时遇到错误。 The exception message is 'Incoming message for operation 'ProcessRequestForMessage' (contract 'IRequestHandler' with namespace ' http://tempuri.org/ ') contains an unrecognized http body format value 'Xml'. 异常消息是“操作'ProcessRequestForMessage'的传入消息(合同'IRequestHandler',名称空间为' http://tempuri.org/ '),其中包含无法识别的http正文格式值'Xml'。 The expected body format value is 'Raw'. 预期的正文格式值为“ Raw”。 This can be because a WebContentTypeMapper has not been configured on the binding. 这可能是因为尚未在绑定上配置WebContentTypeMapper。 See the documentation of WebContentTypeMapper for more details.'. 有关更多详细信息,请参见WebContentTypeMapper的文档。 See server logs for more details. 有关更多详细信息,请参见服务器日志。

and my WCF Data Service is as following: 我的WCF数据服务如下:

 public class ContextWrapper : DataAccessDbContext
{
    public ContextWrapper() : base("connection string")
    {

    }
}

[JSONPSupportBehavior]
public class DataMan : EntityFrameworkDataService<ContextWrapper>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.SetEntitySetAccessRule("Studies", EntitySetRights.None);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        config.UseVerboseErrors = true; // TODO - Remove for production?
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }
    protected override void HandleException(HandleExceptionArgs args)
    {
        base.HandleException(args);
    }
}

I also implemented and configured WebContentTypeMapper to bypass mentioned Error as following: 我还实现并配置了WebContentTypeMapper以绕过以下提到的错误:

 public class ContentTypeMapper : WebContentTypeMapper
{
    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        return WebContentFormat.Raw;
    }
} 

Custom binding: 自定义绑定:

   <binding name="XmlMapper">
      <webMessageEncoding webContentTypeMapperType="MARCO.SOA.PCMServiceLib.ContentTypeMapper,MARCO.SOA.PCMServiceLib.Core"/>
      <httpTransport manualAddressing="true"/>
    </binding>
  </customBinding>

Service endpoint: 服务端点:

 <service behaviorConfiguration="Commom2.Behavior" name="MARCO.SOA.PCMServiceLib.DataMan">
    <endpoint address="rest" behaviorConfiguration="Rest.Behavior" binding="webHttpBinding" 
              bindingConfiguration="XmlMapper" contract="System.Data.Services.IRequestHandler">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:2060/PCM/DataMan.svc"/>
      </baseAddresses>
    </host>
  </service>

but it still get exception, I think something went wrong with my configuration. 但它仍然会出现异常,我认为我的配置出了点问题。

Any help would be truly appreciated. 任何帮助将不胜感激。

Thanks in advance. 提前致谢。

okay, after much trouble I finally solved the problem, so we need to initiate factory property for serviceActivation 好吧,经过很多麻烦之后,我终于解决了问题,所以我们需要为serviceActivation初始化factory属性

So my relative address was: 所以我的相对地址是:

<serviceHostingEnvironment>
  <serviceActivations>
.
.
.
   <add relativeAddress="DataMan.svc" service="MARCO.SOA.PCMServiceLib.DataMan"/>
.
.
.
  </serviceActivations>
</serviceHostingEnvironment>

and I have changed it to 我已经将其更改为

<serviceHostingEnvironment>
  <serviceActivations>
.
.
.
    <add relativeAddress="DataMan.svc" service="MARCO.SOA.PCMServiceLib.DataMan" factory="System.Data.Services.DataServiceHostFactory, Microsoft.Data.Services, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
.
.
.
  </serviceActivations>
</serviceHostingEnvironment>

now everything is now working nice. 现在一切都很好。

more info about DataServiceHostFactory 有关DataServiceHostFactory的更多信息

Note: By this we don't need to override GetMessageFormatForContentType of WebContentTypeMapper and force it to return WebContentFormat.Raw or another content format and don't need any customBinding in config file. 注意:通过此方法,我们无需重写WebContentTypeMapper GetMessageFormatForContentType并强制其返回WebContentFormat.Raw或其他内容格式,并且在配置文件中不需要任何customBinding

Thanks to all. 谢谢大家。

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

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