I created a WCF service to upload file and the WCF service is hosted on windows IIS. I also have a client to consume this WCF service over HTTPS and currently the client and the service are on the same machine. The code is shown below:
IService1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService1
{
// 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 IService1
{
[OperationContract]
void UploadFile(RemoteFileInfo request);
// TODO: Add your service operations here
}
[MessageContract]
public class RemoteFileInfo : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FilePath;
/*
[MessageHeader(MustUnderstand = true)]
public string DestinationDirectory;
*/
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
}
Service1.cvs.cs
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;
using System.Diagnostics;
namespace WcfService1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public void UploadFile(RemoteFileInfo request)
{
FileStream targetStream = null;
Stream sourceStream = request.FileByteStream;
//string destinationDirectory = request.DestinationDirectory;
Console.WriteLine(request.FilePath);
string fileName = Path.GetFileName(request.FilePath);
string filePath = Path.Combine("C:\\upload", fileName);
using (targetStream = new FileStream(filePath, FileMode.Create,
FileAccess.Write, FileShare.None))
{
const int bufferLen = 65000;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
{
targetStream.Write(buffer, 0, count);
}
targetStream.Close();
sourceStream.Close();
}
}
}
}
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>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<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>
Client code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClientApp.ServiceReference1;
namespace ClientApp
{
class Program
{
static void Main(string[] args)
{
Service1Client client = new Service1Client();
string FilePath1 = "C:\\Users\\user1\\Desktop\\data.csv";
System.IO.FileStream stream1 =
new System.IO.FileStream("C:\\Users\\user1\\Desktop\\data.csv",
System.IO.FileMode.Open, System.IO.FileAccess.Read);
string dest = "C:\\upload";
client.UploadFile(FilePath1, stream1);
client.Close();
}
}
}
This client code throw exception on the line " client.UploadFile(FilePath1, stream1); " :
An unhandled exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in mscorlib.dll
Additional information: There was no endpoint listening at http://localhost:53673/Service1.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
However, if I launch browser to access the site, I can access Service1.svc successfully(which means I configure the IIS and post the service to IIS correctly) like the screen shot below:
I would like to know where the problem is? Thanks in advance!
Edit:
The orignal client's App.config is below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53673/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
And based on the suggestion, I change it to:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding name="BasicHttpsBinding_IService1" />
</basicHttpsBinding>
</bindings>
<client>
<endpoint address="https://localhost:53673/Service1.svc" binding="basicHttpsBinding"
bindingConfiguration="BasicHttpsBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpsBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
Now, I got another error:
An unhandled exception of type 'System.ServiceModel.CommunicationException' occurred in mscorlib.dll
Additional information: An error occurred while making the HTTP request to https://localhost:53673/Service1.svc. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server.
I don't think there is a problem with the certificate if I can launch the site from browser. In addition, I use self-signed certificate and client and server are on the same machine. I am thinking if it is that I cannot change the client's app.config directly and there is something wrong with the configuration of the service? I noticed that every time I update the service in the client service reference, the http binding will appear in the app.config. I would like to know what is the right way to deal with it?
Change the client app.config file to point to correct address. It currently points to:
<endpoint address="https://localhost:53673/Service1.svc" binding="basicHttpsBinding"
change the address part to one that is masked in screenshot.
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.