C# 运行WCF服务获取错误无法添加服务。服务元数据可能无法访问

C# 运行WCF服务获取错误无法添加服务。服务元数据可能无法访问,c#,web-services,wcf,C#,Web Services,Wcf,我尝试了一个上传文件的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 res

我尝试了一个上传文件的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>

如果这是Windows(R)
请访问您的通信基础服务
检查是否已在指定的位置启用元数据发布
地址。有关启用元数据发布的帮助,请参阅
MSDN文档位于
交换
错误URI:元数据包含
无法解析的引用:
''. 请求的服务,
无法激活“”。看见
有关详细信息,请参阅服务器的诊断跟踪日志。HTTP GET错误
URI:发生了一个错误
正在下载“”。请求
失败,错误消息为:“/”应用程序中出现服务器错误

要使操作UploadFile中的请求成为流,操作必须 只有一个类型为Stream的参数。描述:安 执行当前网站时发生未处理的异常 要求请查看堆栈跟踪以了解有关堆栈的更多信息 错误及其在代码中的起源

异常详细信息:System.InvalidOperationException:中的请求 操作UploadFile要成为流,该操作必须有一个 类型为Stream的参数

源错误:

在执行过程中生成了未处理的异常 当前web请求。关于货物来源和位置的信息 可以使用下面的异常堆栈跟踪来识别异常

堆栈跟踪:

[InvalidOperationException:操作上载文件中的请求将 操作的流必须有一个类型为的参数 流。]
System.ServiceModel.Dispatcher.StreamFormatter.ValidateAndGetStreamPart(MessageDescription messageDescription,布尔isRequest,字符串操作名)+12750641 System.ServiceModel.Dispatcher.OperationFormatter..ctor(OperationDescription 说明,布尔isRpc,布尔isEncoded)+457
System.ServiceModel.Dispatcher.DataContractSerializePropertyFormatter..ctor(操作说明 说明,DataContractFormattribute DataContractFormattribute, DataContractSerializePropertyBehavior serializerFactory)+58
System.ServiceModel.Description.DataContractSerializerOperationBehavior.GetFormatter(OperationDescription 操作、布尔值和格式请求、布尔值和格式回复、布尔值 isProxy)+217
System.ServiceModel.Description.DataContractSerializePropertyBehavior.System.ServiceModel.Description.IOOperationBehavior.ApplyDispatchBehavior(OperationDescription 描述、调度操作调度)+58
System.ServiceModel.Description.DispatcherBuilder.BindOperations(合同描述 合同,客户端运行时代理,调度运行时调度)+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.Channel.CommunicationObject.Open(时间跨度 超时)+308
System.ServiceModel.Channel.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

我想知道我的代码出了什么问题?

根据MSDN

流媒体的编程模型很简单。要接收流数据,请指定具有单个流类型输入参数的操作约定。要返回流数据,请返回流引用

流式传输时,方法中不能有其他参数。使用附加的fileName参数会导致异常

您可以使用消息约定完成相同的任务,并将其他字段作为类的属性传递,只要流中只有一个属性

[MessageContract]
public class UploadStreamMessage
{
   [MessageHeader]
   public string fileName;
   [MessageBodyMember]
   public Stream fileContents;
} 
您是否阅读了错误消息:“要使operation UploadFile中的请求成为流,操作必须有一个类型为stream的参数”。您的方法有两个参数:
stringuploadfile(stringfilename,streamfilecontents)