2

我想创建 WCF Web 服务来重新计算 .xlsx 和 .xls 文件的公式(想确定服务中的扩展类型并将处理后的 fileID 返回给客户端,然后使用另一个服务方法从返回的 fileID 中获取文件

在此处输入图像描述

但我无法实现我的第一种方法。

我创建了我的服务如下

对象/消息合约

[MessageContract]
    public class UploadStreamMessage
    {
        [MessageHeader]
        public string fileName;
        [MessageBodyMember]
        public Stream fileContents;
    }

界面

[OperationContract]
[WebInvoke(UriTemplate = "/UploadFile")]
string UploadFile(UploadStreamMessage message);

[OperationContract]
Stream ReturnFile(string GUID);

服务方式

public string UploadFile(UploadStreamMessage message)
{
   string FileId = Guid.NewGuid().ToString();
   //Get fie stream and determin the extension type and save in server and return Saved file Id
   return FileId;
}
public Stream ReturnFile(string GUID)
{
   Stream generatedFileStream = null;
   //Get fie using Id and create stream and send back
   return generatedFileStream;
}

网络配置

<bindings>
        <webHttpBinding>
            <binding name="webHttpBinding" transferMode="Streamed"/>
        </webHttpBinding>
    </bindings>
    <behaviors>
        <endpointBehaviors>
            <behavior name="webHttpBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior>
                <!--<behavior name="ServiceBehavior">-->
                <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                <serviceDebug includeExceptionDetailInFaults="true"/>
                <serviceThrottling maxConcurrentCalls="2147483647"  maxConcurrentSessions="2147483647"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

期望: ReturnFile:按预期工作并得到结果 UploadFile:当我尝试运行 UploadFile 方法时,发生了以下异常。

无法加载操作“UploadFile”,因为它具有 System.ServiceModel.Channels.Message 类型的参数或返回类型,或者具有 MessageContractAttribute 和其他不同类型参数的类型。当使用 System.ServiceModel.Channels.Message 或带有 MessageContractAttribute 的类型时,该方法不得使用任何其他类型的参数。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

所以我浏览了 Stackoverflow 并找到了下面的线程 How to return value using WCF's MessageContract? , WCF - Return Object With Stream Data , MessageContract 的使用在启动时使 WCF 服务崩溃, 并发现我在发送消息合同时无法返回字符串值,但可以返回另一个 MessageContract.through Web 服务方法。

所以我改变了我的代码如下向消息合约 添加了一个新的参数(returnFileName),我需要返回给客户端:

[MessageContract]
    public class UploadStreamMessage
    {
        [MessageHeader]
        public string fileName;
        [MessageBodyMember]
        public Stream fileContents;
        [MessageHeader]
        public string returnFileName;
    }

接口和方法如下: 接口:

[OperationContract]
        [WebInvoke(UriTemplate = "/UploadFile")]
        UploadStreamMessage UploadFile(UploadStreamMessage message);

服务方式:

public UploadStreamMessage UploadFile(UploadStreamMessage message)
        {
            message.returnFileName = Guid.NewGuid().ToString();
            string FileId = Guid.NewGuid().ToString();
            //Get fie stream and determin the extension type and save in server and return Saved file Id
            return message;
        }

客户端应用:

static void Main(string[] args)
        {
            ServiceReferenceFile.FileServiceClient Client = new ServiceReferenceFile.FileServiceClient();
            ServiceReferenceFile.UploadStreamMessage message = new ServiceReferenceFile.UploadStreamMessage();
            string fileName = "FileName", outputFile ="";
            Stream str = File.OpenRead("DummyDataFile.xlsx");
            message = Client.UploadFile(ref fileName, ref outputFile, ref str);
        }

但它仍然给我提供了错误并且它不允许返回对象:

无法将类型“void”隐式转换为“ConsoleAppFileAction.ServiceReferenceFile.UploadStreamMessage”

请有人告诉我我在做什么错误?

4

1 回答 1

0

我能够根据@Steeeve 的说明管理代码,并按预期获得结果。 对象/消息合同

[MessageContract]
//It is same as UploadStreamMessage
public class UploadFileRequest
{
  [MessageHeader]
  public string fileName;

  [MessageBodyMember]
  public Stream fileContents;
}

[MessageContract]
public class UploadFileResponse
{
  [MessageBodyMember]
  public string ProcessedFileName;

  [MessageBodyMember]
  public string ProcessedFileNameDetails;
}

界面:

[OperationContract]
UploadFileResponse UploadFile(UploadFileRequest message);

服务方式

public UploadFileResponse UploadFile(UploadFileRequest fileRequest)
{
  UploadFileResponse resp = new UploadFileResponse();
  LogicClass logics = new LogicClass();
  resp.ProcessedFileNameDetails = logics.GetExcelFileMain(fileRequest);
  return resp;
}

客户端应用程序: Web 服务返回的文件名能够作为外参数获取。

ServiceReferenceExcelRefersh.FileServiceClient fileServiceClient = new ServiceReferenceExcelRefersh.FileServiceClient();
    
fileServiceClient.UploadFile(fileName, fileStream, out string ProcessedFileNameDetails);
于 2021-09-13T12:23:38.673 回答