我想創建WCF網路服務來重新計算.xlsx和.xls檔案的公式(想在服務中確定擴展型別并將處理后的fileID回傳給客戶,然后用另一個服務方法從回傳的fileID中獲取檔案
但是我沒能實作我的第一個方法。
我創建了我的服務,如下所示
Object/ MessageContract[MessageContract]
public class UploadStreamMessage
{
[MessageHeader] 。
public string fileName;
[MessageBodyMember]
public Stream fileContents;
}
介面
[OperationContract]
[WebInvoke(UriTemplate = "/UploadFile")/span>]
string UploadFile(UploadStreamMessage message)/span>。
[OperationContract]。
Stream ReturnFile(span class="hljs-built_in">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;
}
Web.Config
<bindings>
<webHttpBinding>
<binding name="webHttpBinding" transferMode="Streamed"/>
</webHttpBinding>
</bindings>
<行為>
<endpointBehaviors>
<行為名稱="webHttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<行為>
<!--<行為名稱="ServiceBehavior">-->
<!-- 為避免披露元資料資訊,設定下面的值為false,然后再部署 -->。
<serviceMetadata httpGetEnabled="true"/span> httpsGetEnabled="true"/>
<!-- 要接收例外細節在故障用于除錯目的,設定下面的值為真。 在部署前設定為false,以避免披露例外資訊 -->
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling maxConcurrentCalls="2147483647" maxConcurrentSessions="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingEnabled="true" />
期望: ReturnFile : 作業和結果符合預期 UploadFile : 當我試圖運行UploadFile方法時,發生了以下例外。
操作'UploadFile'不能被加載,因為它有一個 引數或回傳型別為System.ServiceModel.Channels.Message 或一個具有MessageContractAttribute和其他不同型別引數的型別。 不同型別的引數。當使用System.ServiceModel.Channels.Message或 型別時,該方法不得使用任何其他型別的引數。 型別的引數。描述。在當前網路請求的執行程序中,發生了一個未處理的例外 在當前網路請求的執行程序中發生了未處理的例外。請查看 請查看堆疊跟蹤以了解更多關于該錯誤的資訊,以及它在代碼中的位置。
于是我瀏覽了Stackoverflow,發現了以下執行緒 如何使用WCF的MessageContract回傳值? 。 WCF - 使用流資料回傳物件 。 使用MessageContract使WCF服務在啟動時崩潰。 并發現我在發送訊息合同時不能回傳字串值,但可以通過Web服務方法回傳另一個MessageContract.
。所以我改變了我的代碼,如下所示[]
public class UploadStreamMessage
{
[] 。
public string fileName;
[]
public Stream fileContents;
[]
public string returnFileName;
}
介面和方法如下。 介面 :
[OperationContract]
[WebInvoke(UriTemplate = "/UploadFile")/span>]
UploadStreamMessage UploadFile(UploadStreamMessage message) 。
服務方法:
publicUploadStreamMessage 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訊息。
}
客戶端應用:
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'
。
請告訴我,我做的是什么錯誤?
uj5u.com熱心網友回復: 我能夠根據@Steeeve的指示管理代碼,結果得到了預期。
Objects/ MessageContracts 介面: 服務方法 客戶端應用程式 :
Webservice回傳的檔案名能夠作為外引數獲得。
標籤:[MessageContract]
//It is same as UploadStreamMessage]。
public class UploadFileRequest
{
[MessageHeader ]
public string fileName;
[MessageBodyMember]
public Stream fileContents;
}
[MessageContract] public fileContents; }
public class UploadFileResponse >。
{
[MessageBodyMember] 。
public string ProcessedFileName;
[MessageBodyMember]
public string ProcessedFileNameDetails;
}
[OperationContract]
UploadFileResponse UploadFile(UploadFileRequest message) 。
publicUploadFileResponse UploadFile(UploadFileRequest fileRequest)
{
UploadFileResponse resp = new UploadFileResponse()。
LogicClass logics = new LogicClass()。
resp.ProcessedFileNameDetails = logics.GetExcelFileMain(fileRequest);
return resp;
}
ServiceReferenceExcelRefersh.FileServiceClient fileServiceClient = newServiceReferenceExcelRefersh.FileServiceClient();
fileServiceClient.UploadFile(fileName, fileStream, out string ProcessedFileNameDetails) 。
