【发布时间】:2015-09-09 13:46:31
【问题描述】:
我尝试了一个上传文件的 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) 请访问您有权访问的通信基础服务 检查您是否已在指定位置启用元数据发布 地址。有关启用元数据发布的帮助,请参阅 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 描述,布尔 isRpc,布尔 isEncoded)+457
System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter..ctor(OperationDescription 描述,DataContractFormatAttribute dataContractFormatAttribute, DataContractSerializerOperationBehavior serializerFactory) +58
System.ServiceModel.Description.DataContractSerializerOperationBehavior.GetFormatter(OperationDescription 操作, Boolean& formatRequest, Boolean& formatReply, Boolean isProxy) +217
System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyDispatchBehavior(OperationDescription 描述,DispatchOperation 调度)+58
System.ServiceModel.Description.DispatcherBuilder.BindOperations(合同描述 合约、ClientRuntime 代理、DispatchRuntime 调度)+250
System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription 描述,ServiceHostBase serviceHost) +3171
System.ServiceModel.ServiceHostBase.InitializeRuntime() +65
System.ServiceModel.ServiceHostBase.OnBeginOpen() +34
System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan 超时) +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 结果) +485044
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult 结果)+174
System.ServiceModel.Activation.ServiceHttpHandler.EndProcessRequest(IAsyncResult 结果)+6
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +129
我想知道我的代码出了什么问题?
【问题讨论】:
-
您是否阅读错误消息:“对于将UploadFile操作中的请求作为流,该操作必须有一个类型为Stream的参数”。你的方法有两个参数:
string UploadFile(string fileName, Stream fileContents);
标签: c# web-services wcf