简体   繁体   中英

WCF Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata

I have a WebService that intents to upload a file to a server but i keep getting the error in the title when i want to run it from my VS2010.

I have searched over this site and the solutions have not helped me, unless i'm doing something wrong.

this is the site that i get the example: Example

here is my Interface

namespace FUWcf
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class FileUploadService : IFileUploadService
{
    public bool UploadFileData(FileData fileData)
    {
        bool result = false;
        try
        {
            //Set the location where you want to save your file
            string FilePath = Path.Combine(ConfigurationManager.AppSettings["Path"], fileData.FileName);

            //If fileposition sent as 0 then create an empty file
            if (fileData.FilePosition == 0)
            {
                File.Create(FilePath).Close();
            }

            //Open the created file to write the buffer data starting at the given file position
            using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
            {
                fileStream.Seek(fileData.FilePosition, SeekOrigin.Begin);
                fileStream.Write(fileData.BufferData, 0, fileData.BufferData.Length);
            }
        }
        catch (Exception ex)
        {
            ErrorDetails ed = new ErrorDetails();
            ed.ErrorCode = 1001;
            ed.ErrorMessage = ex.Message;
            throw new FaultException<ErrorDetails>(ed);
        }

        return result;
    }
}
}

Here is the service:

namespace FUWcf
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IFileUploadService
{

    [OperationContract]
    [FaultContract(typeof(ErrorDetails))]
    bool UploadFileData(FileData fileData);
}


[DataContract]
public class FileData
{
    [DataMember]
    public string FileName { get; set; }

    [DataMember]
    public byte[] BufferData { get; set; }

    [DataMember]
    public int FilePosition { get; set; }
}

[DataContract]
public class ErrorDetails
{
    [DataMember]
    public int ErrorCode { get; set; }

    [DataMember]
    public string ErrorMessage { get; set; }
}
}

And here is my web.config

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <appSettings>
    <add key="Path" value="C:\Users\c.asacha\Documents\Proyectos\Generales\FUWcf\FUWcf\temp\"/>
  </appSettings>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHBBinding" />
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="fus" name="FUWcd.FileUploadService">
        <endpoint address=""
          binding="wsHttpBinding" bindingConfiguration="WSHBBinding" name="FileUploadService"
          contract="FUWcf.IFileUploadService" />

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="fus">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

I hope someone can help me with this, or a link for a better example.

Thanks in advance.

You configuration is wrong ( Service name is name= "FUWcd.FileUploadService" where it should be name="FUWcf.FileUploadService" )

Anyway- Created the web site and added the web service like this ( You dont need Mex endpoint in web configuration as it is already on, The url will become the servicename http://{localServerName}:{port}/FileUploadService.svc ):

 public class FileUploadService : IFileUploadService
{
    public bool UploadFileData(FileData fileData)
    {
        bool result = false;
        try
        {
            //Set the location where you want to save your file
            string FilePath = Path.Combine(ConfigurationManager.AppSettings["Path"], fileData.FileName);

            //If fileposition sent as 0 then create an empty file
            if (fileData.FilePosition == 0)
            {
                File.Create(FilePath).Close();
            }

            //Open the created file to write the buffer data starting at the given file position
            using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
            {
                fileStream.Seek(fileData.FilePosition, SeekOrigin.Begin);
                fileStream.Write(fileData.BufferData, 0, fileData.BufferData.Length);
            }
        }
        catch (Exception ex)
        {
            ErrorDetails ed = new ErrorDetails();
            ed.ErrorCode = 1001;
            ed.ErrorMessage = ex.Message;
            throw new FaultException<ErrorDetails>(ed);
        }

        return result;
    }
}

Data Contracts were same as well. Then in Web.Config file added like this and it works fine:

 <system.serviceModel>
<services>
  <service name="WebApplication1.FileUploadService">
    <endpoint address="fileService" binding="wsHttpBinding" bindingConfiguration=""
      contract="WebApplication1.IFileUploadService" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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