c# - HTTP POST XML 到 REST 单例服务抛出 NotSupportedException

标签 c# wcf rest silverlight-2.0

我在 silverlight 应用程序中有以下 REST 主机。我在保存方法的 EndGetResponse() 调用中收到 NotSupportedException。在此示例中,我使用 WCF REST 初学者工具包中的 REST Singleton 服务的默认配置,但对 OnAddItem 方法进行了一些更改以通过 POST 接受更新(因为不支持 silverlight PUT)。当我从 Fiddler 发帖时,我收到 StatusCode 200。该服务托管在与 SilverlightPage 相同的站点上,因此我没有任何 x 域策略设置。我是否遗漏了一些明显的东西?

Load 方法工作正常。

namespace SilverlightApplication2
{
    public class Customer
    {
        public string Name { get; set; }
    }

    public class RestHost
    {
        public event EventHandler Loaded = delegate { };
        public event EventHandler Error = delegate { };

        public Customer Customer { get; set; }
        public string ErrorMessage { get; set; }

        public RestHost()
        {
            Customer = new Customer();

            Load();
        }

        private void Load()
        {
            // begin loading customer
            var request = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:41078/SilverlightApplication2.Web/Service.svc/"));
            request.BeginGetResponse(delegate(IAsyncResult result)
            {
                try
                {
                    var resp = request.EndGetResponse(result);

                    var doc = XDocument.Load(resp.GetResponseStream());

                    Customer.Name = doc.Element("SampleItem").Element("Value").Value;

                    Loaded(this, EventArgs.Empty);
                }
                catch (Exception ex)
                {
                    ErrorMessage = ex.GetBaseException().Message;
                    Error(this, EventArgs.Empty);
                }
            }, null);
        }

        public void Save()
        {
            var request = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:41078/SilverlightApplication2.Web/Service.svc/"));
            request.Method = "POST";
            request.ContentType = "application/xml";
            request.BeginGetRequestStream(delegate(IAsyncResult result)
            {
                var postDoc = new XDocument(
                    new XElement("SampleItem", new XAttribute(XNamespace.Xmlns + "i", "http://www.w3.org/2001/XMLSchema-instance"),
                        new XElement("Value", Customer.Name)
                    )
                );
                postDoc.Save(request.EndGetRequestStream(result));

                request.BeginGetResponse(delegate(IAsyncResult result2)
                {
                    try
                    {
                        var resp = request.EndGetResponse(result2); // NotSupportedExecption here

                        var doc = XDocument.Load(resp.GetResponseStream());

                        Customer.Name = doc.Element("SampleItem").Element("Value").Value;

                        Loaded(this, EventArgs.Empty);
                    }
                    catch (Exception ex)
                    {
                        ErrorMessage = ex.GetBaseException().Message;
                        Error(this, EventArgs.Empty);
                    }
                }, null);
            }, null);


        }
    }
}

这里还有页面代码

<UserControl x:Class="SilverlightApplication2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Width="100" HorizontalAlignment="Left" Margin="15">
            <TextBlock>Customer</TextBlock>
            <TextBox x:Name="CustomerName" />
            <TextBlock Foreground="LightGray" x:Name="LoadingLabel" Margin="5,-19,0,0">Loading ...</TextBlock>
            <Button Content="Save" Margin="0,5,0,0" Click="Button_Click" />
        </StackPanel>
    </Grid>
</UserControl>

代码隐藏

namespace SilverlightApplication2
{
    public partial class Page : UserControl
    {
        private RestHost _host;

        public Page()
        {
            InitializeComponent();

            _host = new RestHost();
            _host.Loaded += delegate
            {
                this.Dispatcher.BeginInvoke(delegate
                {
                    this.LoadingLabel.Visibility = Visibility.Collapsed;
                    this.CustomerName.Text = _host.Customer.Name;
                });
            };
            _host.Error += delegate
            {
                this.Dispatcher.BeginInvoke(delegate
                {
                    HtmlPage.Window.Alert(_host.ErrorMessage);
                });
            };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _host.Customer.Name = this.CustomerName.Text;
            _host.Save();
        }
    }
}

最佳答案

关于c# - HTTP POST XML 到 REST 单例服务抛出 NotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/327996/

相关文章:

rest - 如何在jmeter中使http请求中的搜索参数为动态的

c# - 如何使用 NewtonSoft 更新 JSON 对象的属性

java - Web 服务互操作性

mysql - 具有多个联接的大型查询与多个单个查询

wcf - 将 NInject 与 WCF 服务一起使用时使用 MSMQ 绑定(bind)在 WAS 中托管时替代 HttpContext

c# - 启动 Windows 服务时无法找到程序集文件 'example.dll'

image - 休息 api 设计和工作流程上传图像。

c# - 在 LINQ 中获取组的第一条记录?

c# - 使用 AvalonDock 2.0 时未处理的 'System.ComponentModel.Win32Exception'

c# - 如何从表达式中获取字段名称?