我尝试过上传文件的WCF服务。
以下是代码:
restService.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using System.ServiceModel.Web;
namespace restService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "restService" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select restService.svc or restService.svc.cs at the Solution Explorer and start debugging.
public class restService : IrestService
{
[WebInvoke(Method = "POST", UriTemplate = "UploadFile?fileName={fileName}")]
public string UploadFile(string fileName, Stream fileContents)
{
//save file
try
{
string absFileName = string.Format("{0}\\FileUpload\\{1}"
, AppDomain.CurrentDomain.BaseDirectory
, fileName);
using (FileStream fs = new FileStream(absFileName, FileMode.Create))
{
fileContents.CopyTo(fs);
fileContents.Close();
}
return "Upload OK";
}
catch (Exception ex)
{
return "FAIL ==> " + ex.Message;
}
}
}
}
IrestService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
namespace restService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IrestService" in both code and config file together.
[ServiceContract]
public interface IrestService
{
[OperationContract]
string UploadFile(string fileName, Stream fileContents);
}
}
的web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
当我使用WCF测试客户端测试服务时,我收到错误:
无法添加服务。可能无法访问服务元数据。使 确保您的服务正在运行并公开元数据。
错误:无法从中获取元数据 http://localhost:49202/restService.svc如果这是Windows(R) 请访问您的Communication Foundation服务 检查您是否已在指定的位置启用元数据发布 地址。有关启用元数据发布的帮助,请参阅 MSDN文档在 http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata交流 错误URI:http://localhost:49202/restService.svc元数据包含一个 无法解决的参考: 'http://localhost:49202/restService.svc'。请求的服务, 无法激活“http://localhost:49202/restService.svc”。看到 服务器的诊断跟踪日志以获取更多信息.HTTP GET错误 URI:http://localhost:49202/restService.svc出错了 下载“http://localhost:49202/restService.svc”。请求 失败并显示错误消息: - '/'应用程序中的服务器错误。
对于操作中的请求,UploadFile是操作必须的流 有一个参数,其类型是Stream。描述:一个 在执行当前Web期间发生了未处理的异常 请求。请查看堆栈跟踪以获取有关的更多信息 错误以及它在代码中的起源。
异常详细信息:System.InvalidOperationException:对于请求 操作UploadFile是一个流操作必须有一个 类型为Stream的参数。
来源错误:
执行期间生成了未处理的异常 当前的网络请求。有关的来源和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
堆栈追踪:
[InvalidOperationException:对于操作中的请求,UploadFile为 一个流操作必须有一个类型为的参数 流。]
System.ServiceModel.Dispatcher.StreamFormatter.ValidateAndGetStreamPart(MessageDescription messageDescription,Boolean isRequest,String operationName)+12750641 System.ServiceModel.Dispatcher.OperationFormatter..ctor(OperationDescription description,Boolean isRpc,Boolean isEncoded)+457
System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter..ctor(OperationDescription description,DataContractFormatAttribute dataContractFormatAttribute, DataContractSerializerOperationBehavior serializerFactory)+58
System.ServiceModel.Description.DataContractSerializerOperationBehavior.GetFormatter(OperationDescription 操作,布尔&amp; formatRequest,Boolean&amp; formatReply,Boolean isProxy)+217
System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyDispatchBehavior(OperationDescription 说明,DispatchOperation dispatch)+58
System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription 合同,ClientRuntime代理,DispatchRuntime调度)+250
System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description,ServiceHostBase serviceHost)+3171
System.ServiceModel.ServiceHostBase.InitializeRuntime()+65
System.ServiceModel.ServiceHostBase.OnBeginOpen()+34
System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)+49
System.ServiceModel.Channels.CommunicationObject.Open(时间跨度 超时)+308
System.ServiceModel.Channels.CommunicationObject.Open()+36
System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo,EventTraceActivity eventTraceActivity)+90
System.ServiceModel.HostingManager.EnsureServiceAvailable(字符串 normalizedVirtualPath,EventTraceActivity eventTraceActivity) +598 [ServiceActivationException:由于编译期间发生异常,无法激活服务'/restService.svc'。例外 消息是:对于操作中的请求,UploadFile是一个流 操作必须有一个参数,其类型为Stream ..]
System.Runtime.AsyncResult.End(IAsyncResult result)+485044
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult的 结果)+174
System.ServiceModel.Activation.ServiceHttpHandler.EndProcessRequest(IAsyncResult的 结果)+6
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult的 ar)+129
我想知道我的代码出了什么问题?
答案 0 :(得分:2)
根据MSDN https://msdn.microsoft.com/en-us/library/ms733742(v=vs.110).aspx
流媒体的编程模型很简单。要接收流数据,请指定具有单个Stream类型输入参数的操作协定。要返回流数据,请返回Stream引用。
流式传输时,您的方法中不能包含其他参数。有一个额外的fileName参数导致异常。
您可以使用消息合同来完成同样的事情并将其他字段作为类的属性传递,只要只有一个属性是Stream。
3003