I am learning WCF. I have an ASP.NET MVC2 web app, and I want to send mime-formatted emails from the app so I thought I'd encapsulate the email functionality in a separate WCF project for future reuse etc. I therefore added a WCF Service Library to the solution, coded the email functionality, added a Service Reference to the web project, and it all works fine when run from within VS. However, if I access the web app from a browser window (and not via Visual Studio 'F5'), then the WCF service does not work. I get the following error:
Unable to connect to the remote server at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream() No connection could be made because the target machine actively refused it 127.0.0.1:8732 at System.Net.Sockets.Socket.DoConnect
The web.config file in the web app was amended (by WCF) as follows:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IEmailService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="开发者_高级运维Text"
textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8732/Design_Time_Addresses/Email/EmailService/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IEmailService"
contract="EmailServiceReference.IEmailService" name="WSHttpBinding_IEmailService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
What do I need to do to get the app+service to work outside of VS2010? Also, any deployments tips would be greatly appreciated for when I eventually deploy to our servers.
Thanks.
Edit Thanks for all the answers. Given that I am going to deploy this on the web, should I create a separate web app or web service which then references the service library, and which i would then deploy to an independent web server in due course?
If you deployed your service to your local machine, most likely either the port the service is running on is blocked by the firewall or you are looking at the wrong port. Don't forget when you deploy the service, the port might be different than what the development environment used.
Also, and maybe more importantly, did you actually deploy the service to your local machine? When the development environment runs (F5), it starts a small version of IIS to run your service in. This does not happen when you try to run it outside of the dev environment. You need to deploy the WCF service somewhere (either your machine for testing or another one). Then you will know what port it is listening on and you can hook up your environment properly. It sounds like you might not have done that step, which is causing your problem.
Here is a great walkthrough tutorial for how to deploy a WCF service:
http://www.dotnetspark.com/kb/1679-how-to-host-wcf-services-tutorial.aspx
The reason is when u run with F5 your wcf service get automatically hosted WcfSvcHost.exe . But when directly access the web page thru localhost (without havign wcf service hosted) you get this error. What you need to do is host the wcf first in IIS ( or anywhere else) and then use the web app.
Here are options for hosting your wcf service.
WCF service library is just project for WCF service classes, contracts etc. It is not a service. When you run the application from visual studio it will see the service and host it in WcfSvcHost.exe (helper tool for development) but in case of real run you must host the service yourselves.
精彩评论