我正在尝试通过HTTP POST调用WCF服务,但该服务返回400错误.我不知道这是由于OperationContract还是我正在进行POST的方式.这就是合同在服务器端的样子:
[OperationContract, WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]
Stream Download(string username, int fileid);
Run Code Online (Sandbox Code Playgroud)
以下是我试图通过测试控制台应用程序调用服务的方法:
HttpWebRequest webRequest = WebRequest.Create("http://localhost:8000/File/Download") as
HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytes = Encoding.ASCII.GetBytes("username=test&fileid=1");
Stream os = null;
webRequest.ContentLength = bytes.Length;
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
WebResponse webResponse = webRequest.GetResponse();
Run Code Online (Sandbox Code Playgroud)
编辑:我应该说清楚我的目标是测试服务,而不是让它接受原始的HTTP POST.如果有更好的方法可以测试服务,请随时分享.
我正在尝试使用jQuery AJAX对在localhost上运行的服务执行POST,但即使在我设置之后它也会继续返回状态代码0 jQuery.support.cors = true.我还可以从浏览器成功导航到我的WCF REST服务.这是我的JavaScript看起来像:
<script>
jQuery.support.cors = true;
$(document).ready(function(){
$.ajax({
type: "POST",
url: "http://localhost:8000/Test",
data: '{"test":"test"}',
contentType: "application/json",
dataType: "json",
success: function (msg) {
alert('success');
},
error:function(x,e){
if(x.status==0){
alert('error 0');
}
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
有谁知道是什么原因引起的?我还要提一下,我不能使用jQuery在localhost上发布任何内容.
据Fiddler说,没有发送JSON数据,而是完成HTTP OPTIONS而不是POST.
我正在尝试关注此博客文章:http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx
我的目标是以某种方式获取传入请求的远程地址,但由于某种原因,地址要么在任何参数中都无法看到,要么为空.
这是我正在实现的界面:
public interface IDispatchMessageInspector
{
object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext);
void BeforeSendReply(ref Message reply, object correlationState);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试获取远程地址的方法是AfterReceiveRequest.我检查了两个参数request和channel.此外,它似乎应该channel.RemoteAddress是它应该是,但由于某种原因该属性为空.该request参数也为null,但我猜这是因为我正在进行GET而不是POST.
下面是我正在调用以测试它的方法的签名.
[OperationContract, WebGet( UriTemplate = "{*path}", ResponseFormat = WebMessageFormat.Json)]
string[] GetList(string path);
Run Code Online (Sandbox Code Playgroud) 尝试使用ImageFormat参数将Bitmap保存到MemoryStream时,我收到一个通用的GDI +错误.
奇怪的是,这只发生在我的一台计算机上,而不是其他计算机上.我注意到的唯一区别是获得异常的计算机在App.config文件中也需要以下内容:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
Run Code Online (Sandbox Code Playgroud)
没有它,它无法启动应用程序(它只是一个托管Windows服务).据我所知,两台计算机都有相同的.NET版本,但我可能没有正确检查.可能是什么导致了这个?
编辑:我没有包含代码,因为我几乎可以肯定这是由其他东西引起的.无论哪种方式,这里都是抛出异常的片段:
MemoryStream stream = new MemoryStream();
Image image = Image.FromStream(new MemoryStream(File.ReadAllBytes(fullpath))); //JPG
image.Save(stream, image.RawFormat); //works fine
//image.Save(stream, ImageFormat.Png); //throws generic GDI+ error
Run Code Online (Sandbox Code Playgroud)
我还应该提到,获得异常的机器运行32位Vista,而其他运行不运行64位Windows 7.每台机器在运行之前编译应用程序本身.
最重要的是,所有机器都有useLegacyV2RuntimeActivationPolicy="true"他们的App.config文件,但只有获得GDI +错误的人才是绝对需要的.
另外,以下是我在两台机器上安装的.NET版本:
可以运行,使用64位Windows 7: 1.0.3705,1.1.4322,2.0.50727,3.0,3.5,4.0.30319,Framework64:2.0.50727,3.0,3.5,4.0.30319
使用32位Vista获取错误: 1.0.3705,1.1.4322,2.0.50727,3.0,3.5,4.0.30319
我有一个充当文件存储的RESTful WCF服务.由于可以有任意数量的目录和子目录,我试图让用户只需将文件路径放入URL即可访问它们.有没有办法我可以做到这一点,而无需用户编码斜杠?
例如,我想要的是一个Files/{path}可以访问的URI模板http://localhost:8000/Files/folder1/subfolder2/subfolder3/file.jpg.
在我的程序中使用Outlook interop时出现间歇性错误.我让用户时不时地报告这个错误,但是不可能在我的最终重现.更奇怪的是,如果他们重新启动程序并再次尝试,则错误消失了.
这是我用来获取Outlook引用的代码.
public class CommonStuff
{
public static void Initialize()
{
olk = new Microsoft.Office.Interop.Outlook.Application();
olk.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(OutlookInterop_ItemSend);
}
public static Microsoft.Office.Interop.Outlook.Application GetOutlook()
{
if (Process.GetProcessesByName("OUTLOOK").Count() == 0) //previous attempt at fixing this issue, and i'm not sure if i even need this
{
olk = new Microsoft.Office.Interop.Outlook.Application();
}
return olk;
}
}
Run Code Online (Sandbox Code Playgroud)
以及发送实际电子邮件时使用的代码.
public void SendEmail()
{
Microsoft.Office.Interop.Outlook.MailItem eMail = (Microsoft.Office.Interop.Outlook.MailItem)CommonStuff.GetOutlookApp().CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
eMail.Subject = String.Format("Subject");
eMail.To = "email@things.com";
eMail.Companies = "Company";
eMail.Body = "blah blah blah";
eMail.Attachments.Add(GetCrystalReportPDF());
((Microsoft.Office.Interop.Outlook._MailItem)eMail).Display(); …Run Code Online (Sandbox Code Playgroud) 创建WCF服务后,有没有办法在没有IIS的情况下部署它?我创建的服务只会在局域网中使用,而我宁愿让主机运行ASP.NET服务器来托管WCF服务.
我需要能够做到这一点的另一个原因是因为我将使用的其中一个DLL与ASP.NET不能很好地兼容.