繁体   English   中英

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

[英]Run WCF service getting error Failed to add a service. Service metadata may not be accessible

我尝试了上传文件的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-MetadataExchange错误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的单个参数。

源错误:

当前Web请求的执行期间生成了未处理的异常。 可以使用下面的异常堆栈跟踪来标识有关异常的来源和位置的信息。

堆栈跟踪:

[InvalidOperationException:为使操作UploadFile中的请求成为流,该操作必须具有一个类型为Stream的单个参数。
System.ServiceModel.Dispatcher.StreamFormatter.ValidateAndGetStreamPart(MessageDescription messageDescription,布尔值isRequest,字符串operationName)+12750641 System.ServiceModel.Dispatcher.OperationFormatter..ctor(OperationDescription描述,布尔值isRpc,布尔值isEncoded)+457
System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter..ctor(操作说明,DataContractFormatAttribute dataContractFormatAttribute,DataContractSerializerOperationBehavior serializerFactory)+58
System.ServiceModel.Description.DataContractSerializerOperationBehavior.GetFormatter(OperationDescription操作,布尔值和formatRequest,布尔值和formatReply,布尔值isProxy)+217
System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyDispatchBehavior(OperationDescription description,DispatchOperation dispatch)+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(TimeSpan超时)+308
System.ServiceModel.Channels.CommunicationObject.Open()+36
System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo,EventTraceActivity eventTraceActivity)+90
System.ServiceModel.HostingManager.EnsureServiceAvailable(String 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 https://msdn.microsoft.com/en-us/library/ms733742(v=vs.110).aspx

流的编程模型非常简单。 要接收流数据,请指定具有单个Stream类型输入参数的操作协定。 要返回流数据,请返回Stream参考。

流式传输时,方法中不能包含其他参数。 有一个额外的fileName参数会导致异常。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM