【问题标题】:Create a dataContract in separe file problem在单独的文件问题中创建 dataContract
【发布时间】:2019-02-22 23:18:41
【问题描述】:

您好,我需要创建一个类来返回 WCF 服务中的数据。我在5 simple steps to create your first RESTful service 关注了网络。但是我得到了错误。我在网上搜索并添加了 System.Runtime.Serialization.DataContractSerializer,有人告诉我应该怎么做。我正在使用 VS2015 作为构建它的工具。谢谢。

Imports System.Runtime.Serialization
Imports System.Collections.Generic
Imports System.Runtime.Serialization.DataContractSerializer

 <DataContract>

 Public Class Locations
    <DataMember>
    Public Property LocationName As String
    <DataMember>
    Public Property LocationID As Integer

End Class

【问题讨论】:

  • 请显示您的错误和您的&lt;OperationContract&gt;
  • @AlexKudryashev 错误消息是“属性说明符不是完整的语句。使用续行符将属性应用于以下语句。”
  • &lt;DataContract&gt;Class 声明之间的空行;(

标签: asp.net vb.net wcf


【解决方案1】:

能否请您与我分享错误详细信息?

如您所知,我们通常使用数据合约来传输客户端和服务器端都可以识别的复杂数据类型。使数据可以在不同平台之间正常序列化和传输。 对于 WCF 中的 RESTful Web 服务,我们需要使用 Webhttpbinding 构建数据通道并将 Webhttpbehavior 添加到服务端点。 我做了一个demo,希望对你有用。 服务器端。

Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.ServiceModel.Web

Module Module1

    Sub Main()
        Dim uri As New Uri("http://localhost:900")
        Dim binding As New WebHttpBinding()
        binding.CrossDomainScriptAccessEnabled = True
        binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None
        Using sh As New ServiceHost(GetType(MyService), uri)
            Dim se As ServiceEndpoint = sh.AddServiceEndpoint(GetType(IService), binding, uri)
            se.EndpointBehaviors.Add(New WebHttpBehavior())
            sh.Open()
            Console.WriteLine("Service is ready")

            Console.ReadLine()
            sh.Close()
        End Using

    End Sub
    <ServiceContract([Namespace]:="mydomain")>
    Public Interface IService
        <OperationContract>
        <WebGet(ResponseFormat:=WebMessageFormat.Json)>
        Function SayHello() As List(Of Product)
    End Interface
    Public Class MyService
        Implements IService

        Public Function SayHello() As List(Of Product) Implements IService.SayHello

            Dim result = New List(Of Product)() From {
                New Product With {
                    .Id = 1,
                    .Name = "Apple"
                },
                New Product With {
                    .Id = 2,
                    .Name = "Pear"
                }
            }
            Return result
        End Function
    End Class
    <DataContract([Namespace]:="mydomain")>
    Public Class Product
        <DataMember>
        Public Property Id() As Integer
        <DataMember>
        Public Property Name() As String
    End Class

End Module

客户。

$(function(){
$.ajax({
    type:"GET",
    url:"http://10.157.18.188:900/sayhello",
    dataType:"jsonp",
    success:function(d){
     $.each(d,function(i,o){
         console.log(o.Id);
         console.log(o.Name);
     })
    }
})

})

结果。 这是官方示例

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-create-a-basic-wcf-web-http-service

【讨论】:

  • 消息是“属性说明符不是一个完整的语句。使用续行将属性应用于以下语句”
  • 请删除 dataContract 和类定义语句之间的空行。
猜你喜欢
  • 2017-12-04
  • 2011-12-14
  • 2011-10-09
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 2022-07-20
  • 2012-03-30
相关资源
最近更新 更多