简体   繁体   English

Sharepoint 2010自定义WCF服务返回400-使用OpenXML的“错误请求”

[英]Sharepoint 2010 Custom WCF service returns 400 - “bad request” with OpenXML

I'm developing a custom Sharepoint 2010 service to work with Excel files. 我正在开发自定义Sharepoint 2010服务以使用Excel文件。 I'm using VS2015 on my local workstation. 我在本地工作站上使用VS2015。

The service works and debugs just fine getting the SPFile, reading it's properties and converting it into a stream. 该服务可以正常工作并进行调试,只需获取SPFile,读取其属性并将其转换为流即可。 However, as soon as I include the code to create the SpreadsheetDocument using SpreadsheetDocument.Open() it doesn't even debug anymore but simply retuns a response of 400 "Bad Request". 但是,一旦我包含使用SpreadsheetDocument.Open()创建SpreadsheetDocument的代码,它甚至都不再调试,而只是重新调整400“ Bad Request”的响应。

Service Code 服务编号

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.SharePoint;
using System;
using System.IO;
using System.ServiceModel.Activation;

namespace Lifeco.Corp.Sharepoint
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ExcelItemToSapService : IExcelItemToSapService
    {

        public ServiceResult SubmitSpreadsheet(string documentUrl)
        {
            // Ensure we have the neccessary information.
            if (string.IsNullOrEmpty(documentUrl))
            {
                return new ServiceResult() { Status = "error", Message = "List item url is required as the 'documentUrl' parameter." };
            }


            SPFile doc = SPContext.Current.Web.GetFile(documentUrl);

            if (doc == null || !doc.Exists)
            {
                return new ServiceResult() { Status = "error", Message = string.Format("Document item at '{0}' was not found.", documentUrl) };
            }

            using (Stream dataStream = doc.OpenBinaryStream())
            {

                // As is this works.  Uncommenting the following 'using' block and I receive 400 - Bad Request without even getting to step into the code and debug.
                //using (SpreadsheetDocument document = SpreadsheetDocument.Open(dataStream, false))
                //{
                //    // work with spreadsheet...
                //}
            }

            ServiceResult response = new ServiceResult() { Status = "success" };
            response.Message = string.Format("Title: {0} | Version: {1} | Modified By: {2}", doc.Title, doc.UIVersionLabel, doc.ModifiedBy.Name);

            return response;
        }
    }
}

.svc .SVC

@ ServiceHost 
    Language="C#" 
    Debug="true" 
    Service="Lifeco.Corp.Sharepoint.ExcelItemToSapService, $SharePoint.Project.AssemblyFullName$" 
    CodeBehind="ExcelItemToSapService.svc.cs" 
    Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

The error is received the same whether calling the service directly in the browser or with the following jquery on a Sharepoint page 无论是直接在浏览器中调用服务还是在Sharepoint页面上使用以下jquery,都会收到相同的错误

$.ajax({
                type: "GET",
                url: webServerRelativeUrl + '/_vti_bin/ExcelItemToSapService/ExcelItemToSapService.svc/SubmitSpreadsheet',
                contentType: "application/json; charset=utf-8",
                data: { "documentUrl": "http://s040997/Corporate/Insurance Risk Sample 2.xlsm" },
                dataType: 'json',
                success: function (data) {
                    //alert('Success\n' + JSON.stringify(data));
                    $resultEl.html('<pre>' + JSON.stringify(data) + '</pre>');
                },
                error: function (jqXHR, status, error) {
                    $resultEl.html('');
                    alert('Error\n' + jqXHR.responseText + '\nstatus: ' + status);
                    //alert('Error\n' + jqXHR.responseText + '\nheader: ' + jqXHR.getResponseHeader() + '\nerror: ' + error);
                }
            });

Any thoughts? 有什么想法吗?

Thanks 谢谢

Figured out the issue. 找出问题。

I needed to add the DocumentFormat.OpenXml.dll as an Additional Assembly to my Sharepoint package. 我需要将DocumentFormat.OpenXml.dll作为附加程序集添加到我的Sharepoint包中。

  1. Open /Package/Package.package from Solution Explorer 从解决方案资源管理器中打开/Package/Package.package
  2. Advanced tab 进阶标签
  3. Add -> Add Existing assembly 添加->添加现有程序集
  4. Entered the source path to the DocumentFormat.OpenXml.dll 输入DocumentFormat.OpenXml.dll的源路径
  5. Selected Deployment Target = GlobalAssemblyCache 选定的部署目标= GlobalAssemblyCache
  6. OK

And the next test succeeded. 下一次测试成功了。

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

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