red5: how can i get current IConnection? - red5

under the function appConnect() i added several attributes to the IConnection class of the current connected user.
i want to fetch that information in other functions, how do i get the IConnection of the current connection while i'm not in appConnect() ?

in each function the following command will provide the IConnection of the current connection:
IConnection conn = Red5.getConnectionLocal();

In your ApplicationAdapter you can also add the connection as the first parameter like so:
public boolean isFoo(String foo)
becomes
public boolean isFoo(IConnection conn, String foo)

Related

How can i assign a App Setting to Private Constant

I am storing some program values in my Web.config file and would like to use them in my Code.
When i try to set the value like this.
Private Const Security As String = ConfigurationManager.AppSettings("jwtKey")
i get the error Constant expression is required. Is there a way to make this work or do i have to assign the value to each function in my controller which needs access to this constant.
An option is to use the ReadOnly attribute:
Private ReadOnly Security As String = ConfigurationManager.AppSettings("jwtKey")
From the MSDN article:
Specifies that a variable or property can be read but not written.
Just what you are describing, assign a value to a variable but do not allow it to be changed.
This what a constructor is for
Class MyController
Private Const Security As String
Public Sub New
Security = ConfigurationManager.AppSettings("jwtKey")
End Sub
If you're using DI, you could pass all the relevant options in as a single object

How can I create a generic class that only works for classes that support certain interface?

Class cacheable(Of T As haveTimeStampandMainStringKey)
Public ReadOnly Property Cache As T
Public ReadOnly Property timestamp As Date
Public Shared Function create(cache1 As T) As cacheable(Of T)
Dim a = New cacheable(Of T)
a._Cache = cache1
a._timestamp = Now
Dim key = T.mainkey 'this things fail to compile
Return a
End Function
End Class
Interface haveTimeStampandMainStringKey
ReadOnly Property TimeStamp As DateTime
ReadOnly Property mainKey As String
End Interface
Basically I want class cacheable to work only with classes that support haveTimeStampandMainStringKey
Yet
Dim key = T.mainkey produces an error
Clearly T supports haveTimeStampandMainStringKey interface. So I should be able to access T.mainkey. I can't. Why? What's wrong with the code?
Why?
It doesn't work because T is a type, not an instance. You need to have an instance to refer to mainKey. You probably want either a.Cache.mainKey or cache1.mainKey.
(If you really want something Shared rather than something attached to an instance, unfortunately, there isn't a good way to do it as it's not supported by .NET except through various reflection-based approaches, see various lamentations about the absence of "static interfaces" over the years.)

How to call a Visual Basic API with parameters

I have the following controller:
Public Class GuestController
Inherits ApiController
Public Function SaveValue(<FromUri> ByVal passedName As String) As Savedata
'Get guest data from database
Dim guestResult As New Savedata
guestResult.Name = passedName.ToLower()
Return guestResult
End Function
End Class
And I have the following model:
Public Class Savedata
Public Property ID As Integer
Public Property Name As String
End Class
Just as a test, I want to be able to call this API passing it a value of JOE and have the API return joe to me.
First I tried
http://localhost:12976/api/guest?a=JOE
But that gave me this error:
No HTTP resource was found that matches the request URI 'http://localhost:12976/api/guest?a=JOE'.
No action was found on the controller 'Guest' that matches the request.
Then I found that I needed to name the url parameter the same as the parameter I have defined in my ApiController function. So, for my second attempt, I sent in this:
http://localhost:12976/api/guest?passedName=JOE
But that gave me this error:
The requested resource does not support http method 'GET'.
So now I'm confused as I don't specify an http method at all.
Ideas? Suggestions?
Thanks.
By default if making a request from the browser, unless otherwise stated, the request will be a GET request
But as the name of the action is not following default naming conventions (like having a Get prefix), the framework is unable to infer which HTTP verb the action accepts.
Use the HttpGet attribute on the action to let the framework know that the action accepts HTTP GET requests
Public Class GuestController
Inherits ApiController
<HttpGet()> _
Public Function SaveValue(<FromUri> ByVal passedName As String) As Savedata
'Get guest data from database
Dim guestResult As New Savedata
guestResult.Name = passedName.ToLower()
Return guestResult
End Function
End Class

How do I make a custom class available to client through a web service

I have a simple web service (asmx)built in net framework 4.0 that has multiple custom classes defined. I would like to know how to make these classes available to the client. one of the classes would be returned from a method but the others are used to pass information to the web service so the client needs to know what is included in the class. for instance
Public Class clsAddress
Public AddressType As TypeAddress
Public Location As TypeLocation
Public Line1 As String
Public Line2 As String
Public City As String
Public State As String
Public ZipCode As String
Public ZipPlus4 As String
End Class
So the client would do something like this to send the info to the webservice:
Dim tempAddressInfo As New clsAddress
tempAddressInfo.AddressType = cboAddressType.SelectedIndex 'TypeAddress.current
tempAddressInfo.Location = cboLocation.SelectedIndex 'TypeLocation.USA
tempAddressInfo.Line1 = txtAddress1.Text
tempAddressInfo.Line2 = txtAddress2.Text
tempAddressInfo.City = txtCity.Text
tempAddressInfo.State = txtState.Text
tempAddressInfo.ZipCode = txtZipCode.Text
tempAddressInfo.ZipPlus4 = txtZipPlus4.Text
mclsIdVerify.pAddressInfo = tempAddressInfo
I saw something about returning a custom class using the XMLInclude attribute in a method that returns the class. what needs to be completed so the custom classes can be sent and received by the client.
TIA
I'd normally leave a question like this to someone with more knowledge but as no one has attemped to give you an answer I'll give it a shot.
I think to be able to convert your class into an object state that can be stored or transfered across a network it needs to be serealizable. This allows it to be reconstructed at the other end.
Try putting _ above your class definition.
Even if this doesnt help hopefully it gives your something to look into.

Taking the connection string value from textbox

In one class I have defined the connection string like this
SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder();
objConnectionString.DataSource = localServer; ;
objConnectionString.UserID = userName;
objConnectionString.Password = password;
objConnectionString.InitialCatalog = selectedDatabase;
where local server = txtHost;--DataSource
userName = txtUsername;
password = txtPassword;
But in my another project I want to access the controls of that project
Currently I am connected with the db like this
using(var sConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]))
But I want to make it such that it would take the value directly from the textboxes used in another project
Waiting for your suggestions .....Can It be done..
You will not bne abble to do this, unless you pass the actual controls to the method in the other project.
Why not rather pass the SqlConnectionStringBuilder object that you set up before hand to the method being called?
In your form with the textbox you will need to create Properties to access the values from the form, e.g.
public string Server
{
get
{
return this.txtHost.Text;
}
}
You will also need to pass a reference of the Form to your other Project, either by referencing the project, or using an shared interface between the two.
In your project where you want to build the connection string, you will need some way of receiving the reference to the Form, such as
public void RunMyQuery(MyForm form)
{
var objConnectionString = new SqlConnectionStringBuilder();
objConnectionString.DataSource = form.Server;
}
If you have time, consider creating a new project which contain shared interfaces, so you could create an interface such as
public interface IConnectionStringPartProvider
{
string Server { get; }
... other parts
}
and implement this interface on your form
public partial class Form1 : Form, IConnectionStringPartProvider
Then you would not need to reference you form project in your logic class, just let both projects reference the shared project.
This way, your query method could be replaced with
public void RunMyQuery(IConnectionStringPartProvider provider)
{
var objConnectionString = new SqlConnectionStringBuilder();
objConnectionString.DataSource = provider.Server;
}