WCF电子邮件服务-获取错误“远程服务器返回了意外的响应:(400)错误的请求". [英] WCF Email service - Getting error "The remote server returned an unexpected response: (400) Bad Request"
问题描述
正在获取主题中描述的错误,它仅在测试控制台应用程序将非常大的附件发送到电子邮件服务时才会发生,即使附件在允许的大小之内,我猜我也有这样的问题测试控制台应用程序中的附件很大,因为我从测试控制台应用程序中收到错误(在该服务无法使用之前),否则我的配置有问题,请帮忙.
Getting error described in the Subject, it only happens when a very large attachment is being sent by the Test Console app to the Email service, even though the attachment is within the permitted size, I am guessing I have a problem with such very large attachment at the Test Console app because I get the error from the Test Console app (before it hits the service), otherwise I have something wrong in my configuration, please help.
// Here is the Test Console application that is designed to send an email with very large attachment to the Email Service:
EmailSerClient proxy = new EmailSerClient();
EmailMessage emailMsg = new EmailMessage();
emailMsg.EmailParamHeaders = new DataContracts.Email();
emailMsg.EmailParamHeaders.To = new string[2];
emailMsg.EmailParamHeaders.CC = new string[1];
emailMsg.EmailParamHeaders.From = "noreply@xys.com";
emailMsg.EmailParamHeaders.To[0] = "abc@xys.com";
emailMsg.EmailParamHeaders.Subject = "Test line here";
emailMsg.EmailParamHeaders.Message = "test message here.";
emailMsg.EmailParamHeaders.AttachmentFileName = @"c:\log\attchment.zip"; //null;
string filePath = System.IO.Path.Combine(@"c:\log\", "attachment.zip");
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
// open stream
System.IO.FileStream stream = new System.IO.FileStream(@"c:\log\attachment.zip",
System.IO.FileMode.Open, System.IO.FileAccess.Read);
// See RemoteFileInfo class below:
RemoteFileInfo result = new RemoteFileInfo();
// return result
result.FileName = "attachment.zip";
result.Length = fileInfo.Length;
result.FileByteStream = stream;
try
{
proxy.SendEmail(emailMsg.EmailParamHeaders, result.FileByteStream);//Stream.Null);
}
catch (Exception e)
{
}
}
// Here is the RemoteFileInfo class which I think might be part of the problem:
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.ServiceModel.Web;
using System.ServiceModel;
namespace wcfTestConsoleApp
{
[MessageContract]
public class RemoteFileInfo : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public long Length;
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
}
// Here is the config file for the Test Client above:
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IEmailService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="50000000" maxArrayLength="50000000" maxBytesPerRead="50000000" maxNameTableCharCount="50000000" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
<message algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/EmailWcfService/EmailService.svc"
binding="basicHttpBinding" bindingConfiguration=""
contract="IEmailService" name="BasicHttpBinding_IEmailService">
<identity>
<servicePrincipalName value="host/abcdefg.com" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
// Here below is the Email service's config file in relation to serviceModel/binding...etc.:
<pre lang="xml"><system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="EmailWCFServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="httpLargeMessageStream" transferMode="StreamedRequest" maxReceivedMessageSize="52428800" maxBufferSize="52428800"/>
</basicHttpBinding>
</bindings>
<services>
<service name="EmailService" behaviorConfiguration="EmailWCFServiceTypeBehaviors">
<endpoint address="" binding="basicHttpBinding" contract="IEmailService" bindingNamespace=""/>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer
推荐答案
首先,我将在您的wcf服务上设置跟踪:
http://msdn.microsoft.com/en-us/library/ms733025.aspx [ ^ ]
一旦设置好,您就可以看到生成的日志,并且可以更好地了解实际问题.
还值得添加以下内容:
1)在端点行为和服务行为中设置最大对象图
Firstly I would setup tracing on your wcf service:
http://msdn.microsoft.com/en-us/library/ms733025.aspx[^]
Once this is in place you can see the logs generated and you get a better idea of the actual problem.
It would also be worth adding the following:
1) setting the max object graph in the endpoint behavior and service behavior
<servicebehaviors>
<behavior name="EmailWCFServiceTypeBehaviors">
<datacontractserializer maxitemsinobjectgraph="2147483647" />
<servicemetadata httpgetenabled="True" />
<servicedebug includeexceptiondetailinfaults="True" />
</behavior>
</servicebehaviors>
<endpointbehaviors>
<behavior name="endpointBehavior">
<datacontractserializer maxitemsinobjectgraph="2147483647" />
</behavior>
</endpointbehaviors>
2)设置端点和服务以使用行为.
3)(可选)增加http请求超时.
2) set the endpoint and service to use the behaviors.
3) optionally increase the http request timeout.
<system.web>
<httpruntime executiontimeout="180" />
</system.web>
这篇关于WCF电子邮件服务-获取错误“远程服务器返回了意外的响应:(400)错误的请求".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!