We are using the webRequestHandler class successfully in an ASHX handler file but I am unable to access it from a new .vb class file I'm trying to create.
I am familiar with this answer but the .dll is already a reference in my project
The type or namespace name "WebRequestHandler" could not be found
'api.vb
Imports System.Net.Http
Public Class api
public function postAPI() as string
dim handler = New WebRequestHandler() 'Type "WebRequestHandler" is not found
[...]
End Function
End Class
The hander is essentially the same but works fine:
'httpHandler.ashx
Imports System.Net.Http
Public Class apiWebHandler
Implements System.Web.IHttpHandler
Private Function getAPI() As String
Dim handler = New WebRequestHandler() 'works like a charm no compiler issues
[...]
End Function
End Class
Any idea why the WebRequestHandler isn't available here? If it isn't available in a class file what would you recommend we do to send certs via web request?
Related
Hi I need to create a class for return the data in WCF service. I followed the web at 5 simple steps to create your first RESTful service. However I get the error for . I searched the web and add the System.Runtime.Serialization.DataContractSerializer, Would someone tell me what should do. I am using VS2015 as the tool to build it. Thanks.
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
Could you please share the error details with me?
As you know, we usually use the datacontract to transmit the complex data type which could be recognized by the client-side and server-side. so that the data could be serialized and transmitted normally between different platforms.
For the restful web service in WCF, we need to use the Webhttpbinding build the data channel and add the Webhttpbehavior to the service endpoint.
I have made a demo, wish it is useful to you.
Server-side.
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
Client.
$(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);
})
}
})
})
Result.
Here is an official sample
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-create-a-basic-wcf-web-http-service
I have vb.net web api controller that I am trying to invoke but I'm getting back the following:
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:26944/api/employee/GetPerson/'.","MessageDetail":"No action was found on the controller 'Employee' that matches the request."}
This is the web controller:
Public Class EmployeeController
Inherits ApiController
Private ReadOnly dbContext As MyEntities
Sub New()
Me.dbContext = New MyEntities
End Sub
<HttpGet>
<ActionName("GetPerson")>
Function Person(ByVal missionaryId As Integer) As IPRS_Data.getPersInfoDetail_Result
Return Me.dbContext.getPersInfoDetail(missionaryId).First
End Function
End Class
WebApiConfig:
Public Shared Sub Register(ByVal config As HttpConfiguration)
' Web API configuration and services
' Web API routes
config.MapHttpAttributeRoutes()
config.Routes.MapHttpRoute(
name:="DefaultApi",
routeTemplate:="api/{controller}/{id}",
defaults:=New With {.id = RouteParameter.Optional}
)
Dim xmlFormat = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(Function(t) t.MediaType = "application/xml")
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(xmlFormat)
End Sub
I'm invoking the service using: appbase/api/employee/GetPerson/
Your method is decorated with HTTPGET and Actionname attribute. You don't need that if you have your method name starting with "Get" (like GetPerson). However, the Actionname is obsolete as it is not considered in your actual routing. Your routing is "api/{controller}/{id}". If you want your action name being considered you need to modify your routing to "api/{controller}/{action}/{id}". And if you want to have your id-param being considered per default routing you should rename the param in your method from missionaryId to just id.
Function Person(ByVal id As Integer) As IPRS_Data.getPersInfoDetail_Result
Return Me.dbContext.getPersInfoDetail(id).First
End Function
And that's the way how to invoke it (don't forget to pass an Id because there is no other "GET" method in your controller which works paramless.
appbase/api/employee/15
or
appbase/api/employee?id=15
and if you insist on missionaryId
appbase/api/employee?missionaryId=15
I'm not sure if this is possible but I would like to associate a class name reference to a shared member method / property / variable. Consider:
Public Class UserParameters
Public Shared Reference As Object
Public Shared Function GetReference() As Object
Return Reference
End Function
End Class
In another part of the program I would like to simply call UserParameters and have it return Reference either by aliasing GetReference or the variable directly.
I am trying to emulate the Application, Request, or Session variable:
Session(0) = Session.Item(0)
Any suggestions would be greatly appreciated.
You can't return an instance member from a static method directly (the static method can't access instance members because it isn't instantiated with the rest of the class, only one copy of a static method exists).
If you need to setup a class in such a way that you can return an instance from a static method you would need to do something similar to the following:
Public Class SampleClass
Private Sub New()
'Do something here
End Sub
Public Shared Function GetSample() As SampleClass
Dim SampleClass As SampleClass
SampleClass = New SampleClass
SampleClass.Sample = "Test"
Return SampleClass
End Function
Private _SampleString As String
Public Property Sample As String
Get
Return _SampleString
End Get
Private Set(ByVal value As String)
_SampleString = value
End Set
End Property
End Class
Public Class SampleClass2
Public Sub New()
'Here you can access the sample class in the manner you expect
Dim Sample As SampleClass = SampleClass.GetSample
'This would output "Test"
Debug.Fail(Sample.Sample)
End Sub
End Class
This method is used in various places in the CLR. Such as the System.Net.WebRequest class. where it is instantiated in this manner in usage:
' Create a request for the URL.
Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
In my ASP.NET Web Service I've included a module in which their are Public declared variables. Will they be thread-safe? Will they get mixed up upon simultaneous calls?
Those variables are mostly DatsSet, DataTable and SQLDataAdapter..
Partial code of the module:
Imports System.Data.OleDb
Imports System.Diagnostics
Module modCommon
Public bDoLog As Boolean
Public sCurrentODBC As String
Public cn As SqlConnection
Public Query1ds As DataSet
Public Query1 As DataTable
Public Query1adapter As SqlDataAdapter
#scripni
Thanks, as I'm not familiary with your suggestions, I will move everything locally.
Additionally, will the following variables be thread-safe?:
[ToolboxItem(False)]_
Public Class Service1
Inherits System.Web.Services.WebService
Dim sName As String
Dim sCurrentPath As String
[WebMethod()]_
Public Function Capture(ByVal sPath As String) As String
sName = "Joe"
End Function
End Class
If you're using web services than yes, you will have concurency problems when multiple services will try to access the same resource, and SqlConnection is definetly a resource you don't want shared.
You should make sure that you don't have simultaneous calls to the properties (for ex. by wrapping the fields with getters / setters and implementing locks in those methods) or by moving the code to a class and instantiating that class whenever you need it.
I have this class that contains vars for db connection;
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Web.Configuration
Public Class DBVars
Public Shared s As String
Public Shared con As String = WebConfigurationManager.ConnectionStrings("NMMUDevConnectionStr").ToString()
Public Shared c As New SqlConnection(con)
Public Shared x As New SqlCommand(s, c)
Dim r As SqlDataReader
End Class
I import this to my page like this;
Imports DBVars
I'm then able to access these vars from my page.
But if I try to import them into a user control using the same method the variables are not available. Am I making an error or is this expected?
Thanks.
Make sure you're accessing them in your usercontrol like so:
Dim useMe As String = DbVars.con
and not something like
Dim x As DbVars = new DbVars()
dim useMe As String = x.con
You need to do this because you've declared them as Shared (static) and not as Instance variables.