|
Please could somebody help... My brains gone dead
I'm trying to pass the following to a wcf service
public IList<tbItem> GetItemsWhereIn (IList<tbClass> iclass)
{
iServiceClient svc = new iServiceClient();
IList<tbItem> list = svc.FindItems(iclass);
return list;
}
The problem is passing parameter iclass to svc.FindItems
error Cannot convert from system...IList<tbClass> to tbClass[]
The wcf service is setup as
public interface iService{
[OperationContract]
List<tbItem> FindItems(IList<tbClass> iclass);}
And the linq query as
public List<tbItem> FindItems(IList<tbClass> iclass)
{
...linq code working correctly
}
If you could point the way I would be very greatful
|
|
|
|
|
I think you need to tell the Client Service Reference to generate the Client proxy using genertic Lists rather than arrays (Lists<T>, IList<T> and other collection types are normally sent up and down the wire as T[] unless you tell the service reference to generate the proxy with something else:
- Right click your service reference
- Select Configure Service Reference
- In the Collection Type drop down select System.Collections.Generic.List
Note that you can't set the Value to IList<T> as Services don't play well with Interfaces / OO in general.
CCC solved so far: 2 (including a Hard One!)
|
|
|
|
|
Thanks for answer, this did the trick
I just needed to then change this line
IList<tbItem> list = svc.FindItems(iclass.ToList());
|
|
|
|
|
I have a List<MyClass> I use this list to populate a combo box so that the user can select an item from the list, and I can easily grab which one they selected:
onSelectedIndexChanged(...)
{
this.currentItem = this.comboBox.SelectedItem as MyClass;
}
The strange thing is, when you first select an item from the list, the box doesn't display any text. If you select the same item again, it does.
The box will only display the correct text if you select the same item twice in a row, and everything works as normal when I don't set this.currentItem
Upon further testing it seems to happen when I access properties on the instance of MyClass right after selecting it. If I comment out a bunch of code the combo box works fine, although it's only reading a few values nothing is actually being changed, not that I see how it could make any difference.
Does anybody have any idea what's going on?
EDIT: I know what it was, purely my fault. When an item was selected a numeric up down control was set with a value from that item, this triggered the ValueChanged event, which reset the value back to the item, and triggered Sort on my list, which clears out and refills the combo box with the items in the new order. Bit of a pain in the arse really, I must pay more attention in future
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
modified on Friday, October 2, 2009 10:15 AM
|
|
|
|
|
Hi.
I got a server and a client application. Server sends bytes to client in TCP. But the problem is that it's "too fast" I mean in a way. For be more specific. I have objects of a class (in a List) in the server side, I format that to xml string, and then format the string to bytes and send this byte array.
//Client:
NetworkStream = new NetworkStream(SocketClient);
while (ListOfObjectToSendOut.Count > 0)
{
NetworkStream.Write(BytesToSend, 0, BytesToSend.Length);
NetworkStream.Flush();
Thread.Sleep(100);
}
//Server:
TcpClient myClient = new TcpClient("localhost", 8000);
NetworkStream NetworkStream = myClient.GetStream();
byte[] bytes = new byte[3000];
while (true)
{
int IncomingByteLength = NetworkStream.Read(incomingBytes, 0, 3000);
}
You can ask, what's my problem?. Let's say that the Object I want to send is 150 bytes long. And I want to send out 5 Object. My problem that i need to happen this in an order like this:
Write 150 byte
Read 150 byte (and than "unformat" it)
Write 150 byte
Read 150 byte (and than "unformat" it)
and so on.
With this way I could unfomate the 150 byte in the Client side.
BUT!!! In a real world it will write out 150 byte and 150 byte again and so on and THAN the Client will Read 450 or 600 or 750 bytes. It is imponderable. And I CAN NOT unformat 300 or 450 ... to my Object. So the Flush doesn't flush it (I read in msdn that it is ok, cause flush wont do anything with the networkstream)So the problem is that the Server sends out 150 bytes and send out the next 150 bytes so fast that the Client will read those together (for example 300 bytes), which is not good.
I need to READ THIS "PACKAGES" SEPARATE in the client side.
- I can solve this to write a Thread.Sleep(100) but i dont want to do that, It would be to slow!
- I can solve this proble using UDP, but i dont want to use UDP.
- I could solve this porblem with a a solution like:
Take a sign byte after every 150 byte, and seperate in the client side by that sign. And then format the bytes back. But I dont want to do that.
- I tried Socket.NoDelay doesn't help
- I tired close and dispose and than make it again the networkStream in EVERY loop. It doesnt help.
Now all you programmers, here is a big deal, Who is the Best Programmer? What can I do? (I hope you can understand my problem )
Thanks!
|
|
|
|
|
Dataflow control and message separation are important aspects of all serial communication. There are a couple of solutions:
1.
keep the consumer in charge, i.e. let the consumer ask for specific data, only then the producer delivers said data, and finally the consumer reads and processes it.
Pro: is simple
Con: slow-down due to back-and-forth conversation
2.
choose a data format that allows for message separation at the receiver side; e.g. insert a unique character (not always easy or even possible, may require an escape mechanism, etc); or use a "protocol" that predicts the useful length of the message (length+content, as in Pascal strings). So now the producer can produce many messages, they all are separable.
Pro: one-way communication, no slow-down
Con: somewhat more complex as the consumer may read too much data, needs to process what amounts to one message, and then somehow recirculate whatever extra data he already got. Kind of a push back, as in LALR parsers. Easiest way is by copying the remaining data to the beginning of the receiving buffer; there are schemes without copying, a round-robin buffer would be one of them.
You may want or need a real dataflow control on top of (2), as it does not solve the consumer overrun by producer situation.
Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Something which you might find useful is serializing all of your objects to the Stream and deserializing them in order. If you don't want to do that, then perhaps you could repeatedly read the data into a buffer of 150 bytes; as a heads-up, this implies that the data length is known by both sides.
|
|
|
|
|
Luc is leading you in the right direction. In my job we do lots of serial and TCP/IP connection between PC's and various devices. We use both methods he suggests but more often than not the protocols we develop have some start character and stop character to delineate the message. The characters that are meant for this purpose are hex 0x02<stx>(send transmission) and hex 0x03<etx>(end transmission). These characters work well as neither are printable characters so it is unlikely that you would be sending them in a normal message. If you really want to get picky and guarantee that the data you receive is the same as the data you sent you should be using some form of error checking such as cyclic redundancy check. If you want to do error checking I would recommend using the send/acknowledge system so that your client can let the server know if it got good data and if not it can send a negative acknowledgement to let the server know it needs to resend the data.
If you always know how many bytes you are expecting you can always just read that many bytes out of your receive buffer and process them, then go back and read more bytes etc... this might be a simpler solution if you don't want to build some sort of protocol, just be sure that your buffer size is large enough that it won't overflow.
|
|
|
|
|
Thanks everybody for the responses!!!!!!!
|
|
|
|
|
Hi all,
I'm trying to use a web service with authenticated access. The service returns a session token with every response and the token may change during the session. Operations on the service all follow a simple pattern: They take one argument representing the request, and return an object representing the response. Each operation has a specific req/response associated with it, but requests are derived from a common base class and responses from another. The base classes provide access to the headers, so they are sufficient to manage the session.
The business object I'm writing that uses the service will execute code in multiple threads. Rather than duplicate the thread-synchronization and token-maintenance code everywhere I invoke a service operation, I thought I'd use generics and delegates so the generic method would implement all the things that should always happen and call the delegate to do the specifics.
Since the service's methods all take a request derived from APIRequest and return a response derived from APIResponse, they conform to Func<APIRequest, APIResponse> . Some methods, for example logout, take a request object that contains no information except the request header that identifies the session. I would like to be able to use my generic method, called Invoke, like this:
<br />
public void Logout()<br />
{<br />
if (!HasSession) return;<br />
var r = Invoke(globalService.logout);<br />
...<br />
}<br />
<br />
void keepAlive()<br />
{<br />
TimeSpan margin = TimeSpan.FromSeconds(30);<br />
<br />
while (HasSession)<br />
{<br />
if (sessionLifeLeft > margin) Thread.Sleep(sessionLifeLeft - margin);<br />
Invoke(globalService.keepAlive);<br />
}<br />
}<br />
<br />
I declared the following generic methods to make this possible (the implementation details aren't really important for our purposes here, but I include it as-is since I don't understand why it doesn't work the way I want it to):
<br />
protected TResponse Invoke<TRequest, TResponse>(Func<TRequest, TResponse> method) <br />
where TRequest : APIRequest, new()<br />
where TResponse : APIResponse<br />
{<br />
return Invoke<TRequest, TResponse>(method, new TRequest());<br />
}<br />
<br />
<br />
protected TResponse Invoke<TRequest, TResponse>(Func<TRequest, TResponse> method, TRequest req) <br />
where TRequest: APIRequest <br />
where TResponse: APIResponse<br />
{<br />
APIResponse r;<br />
lock (this)<br />
{<br />
req.header = reqHeader;<br />
r = method(req);<br />
watch.Reset();<br />
reqHeader.sessionToken = r.header.sessionToken;<br />
}<br />
return (TResponse)r;<br />
}<br />
I don't understand why I need to cast the return value; the compiler says I can't implicitly convert APIResponse to TResponse. I understand one can't do so in general, but since "method" returns TResponse already I don't understand why such a conversion would result from my code. But with the cast it builds, but when I try to use it as described above the compiler claims the type parameters "cannot be inferred from usage", which I think is weird since there are only two type parameters and both are well-defined from "method", which takes TRequest and returns TResponse.
I have a hunch this has something to do with me thinking in terms of the *formal* parameters rather than the *actual* (run-time) parameters. Still, I'm wondering if there is any way to achieve the desired outcome: to enable invoking the service operations without having to write the type parameters explicitly. It just isn't very elegant and looks rather redundant to code
<br />
Invoke<LogoutReq, LogoutResp>(globalService.logout);<br />
Can anyone think of a way to make the type inference work out as I wish, from the Func<t, tresult=""> types?
|
|
|
|
|
You need to cast the return value because the compiler does not try to make any assumptions about what may or may not happen to r between the time you store the method return to the time you return it from your method. Since you only intend to put TResponse in, why not just change r to be of type TResponse instead of APIResponse.
As for the second part, how is globalService.logout declared?
|
|
|
|
|
Thank you, that's the sort of simple and rather logical explanation I dared not hope for!
The declaration is
<br />
public LoginResp login(LoginReq request)<br />
LoginResp : APIResponse
LoginReq : APIRequest
However, I've found some worse caveats to deal with. The API has been divided into several services, but one needs data from one service to make requests to the other service - as well as the same session token for both as far as I understand, which means one cannot reliably call both services concurrently. A lot of types defined by the WSDL exist in both services. But of course, when I just generate the proxies all these types end up with redundant definitions in different namespaces. Worse, to invoke an operation in service2 that needs input obtained from service1, I have to waste time and space at run-time as well as write rather boring code like this:
<br />
public Service2.SomeType ToService2Type(Service1.SomeType obj)<br />
{<br />
Service2.SomeType result = new Service2.SomeType();<br />
result.Property1 = obj.Property1;<br />
result.Property2 = obj.Property2;<br />
<br />
result.Property3 = ToService2Type(obj.Property3);<br />
...<br />
<br />
return result;<br />
}<br />
<br />
public Service2.SomeOtherType ToService2Type(Service1.SomeOtherType)<br />
{<br />
...<br />
}<br />
Writing the code is a bore, but I think it's even worse that I cannot avoid the run-time overhead. I guess if I go into the generated proxy code I can change attributes all over the place and use the same types with both services where in fact they match the soap messages, but doing so would be even more of a chore and what if I need to regenerate the proxies some day?
I could perhaps write a separate app that uses reflection to discover the types and generate the code for all those ToService2Type methods, but that of course doesn't remove the run-time overhead.
Not quite as nice as I hoped this API...
|
|
|
|
|
WSDL and all of that is way outside of my experience (and doesn't seem to be something you are really having questions about, just complaints), so I won't speak to that.
Doing a search for "generic delegate inference" showed other people that were having the same problem as you, along with some explanations I will rephrase here. I'm sure I've run into the problem before and just don't remember where. The problem seems to come down to the compiler having to infer too many things at once. The rest of this post is how I assume the general flow of compilation goes, but I am by no means an authority on the compiler.
When you say Invoke(globalService.logout) , the compiler first has to guess what type to make globalService.logout , before it can determine what types to use for Invoke . It will then look to the method signature of Invoke to try to determine what type of delegate to create from the method group globalService.logout . It will see that it is based on a generic parameter, so it can't make any assumptions there. In this case, there is only one possible overload of logout, but there could be more logout definitions, and then there would be no way to know which one you want. The compiler team probably decided it wasn't worth adding the special case for when you have only one overload. One reason against that feature would be that adding an overload to the method would suddenly break previously working code.
Since there are no other parameters, the compiler has no more information to help it determine what types to fill in for the generic declaration and gives up. If you were to change Invoke to be Invoke<TRequest, TRepsone>(Func<TRequest, TResponse> method, TRequest param) , the compiler would then figure it out from the second parameter. It can determine this because you are not allowed to overload methods solely by return type. Thus when it sees the second parameter is, for example, string , the first parameter becomes Func<string, TResponse> which is apparently just enough for it to do the rest of the work itself. As you might guess, giving Invoke a parameter of TResponse will not clear up the error (because you could still have multiple definitions that would match).
|
|
|
|
|
I've referenced a dll made in VB.Net and now I need to inherit from some of the classes in the dll. Below is an example of how I did that:
class Tag : DataProvider.Trend.Tag
{
}
Now if I do like this:
DataProvider.Trend.Tag myBaseTag = new DataProvider.Trend.Tag();
Tag myTag = myBaseTag as Tag;
this results in that 'myTag' gets the value null. Why is that? How can I assign the value of myBaseTag to myTag?
Thanks for help!
|
|
|
|
|
Tag myTag = new Tag();
DataProvider.Trend.Tag myBaseTag = myTag as DataProvider.Trend.Tag;
Because myTag inherite from DataProvider.Trend.Tag.
|
|
|
|
|
Thanks for the reply.
But the problem for me is that I call methods in the dll I mention and they return the type 'DataProvider.Trend.Tag' BUT I need to cast that to type 'Tag'. I don't want to cast 'Tag' to 'DataProvider.Trend.Tag' (the reason is that 'Tag' so that it is accepted by a Web Service).
Thanks again.
|
|
|
|
|
You can cast an object of type A to a type B that inherits from A only when the object was already declared as B.
What you want to achieve is possible by creating a new object of type Tag and then initializing all its properties by using values from the 'DataProvider.Trend.Tag' original object (you can create a copy constructor for this).
|
|
|
|
|
xkrja wrote: DataProvider.Trend.Tag myBaseTag = new DataProvider.Trend.Tag();
Tag myTag = myBaseTag as Tag;
Of course that does not work. You can cast down, you cannot cast up. You need to read a basic book on OO
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
That's not going to work 'out of the box', you need to provide an explicit/implicit conversion method if you want to upcast.
|
|
|
|
|
Say we have
class A { protected string field1; }
class B : A { protected string field2; }
class C : B { protected string field3; }
void foo(A a) {}
void bar(B b) {}
void foobar(C c) {}
Now consider what instances of these objects *actually* will be inside your hardware. (We can safely ignore the fact that A extends object for the purpose of looking at why your attempted cast doesn't make sense.)
First a word about references: A reference is what we might also call a "managed pointer": Like a pointer, it simply contains the address where the data is stored, but unlike a pointer, we cannot obtain this address and the memory manager can move stuff around and update pointers as needed without us knowing anything about it.
Since string is a reference type, an instance of A is a word of memory (4 bytes on a 32-bit system) containing a reference to string data. An instance of B however is two words of data, containing two references, to field1 and field2 data. And C of course is three words, laid out in memory like an instance of B appended with the additional reference to field3 data.
What you want to do is the equivalent of trying to use an instance of A and pass it to foobar, which takes a parameter of type C. Since an A is just the word for the field1 reference, foobar might access memory outside the bounds of the object and it could screw up other live objects (placed next to the instance of A in memory). So clearly the compiler shouldn't let you do that.
If you wanted the compiler to just go ahead and create an instance of C for you when you make the cast, it wouldn't know what to do with field2 and field3. There is no way *in general* to know that the MotorBike class (descendant type) works just fine with only the data it inherited from the Vehicle class. If there were, we could just go like this:
AmazingWebSerive s = (AmazingWebService)(new object());
Sadly, this doesn't work. Instead, it's you as a programmer who may know this and provide a way to construct a B with only A data, or a C with only A data, and so on. Then you decide what the defaults should be for the data that doesn't exist in A.
class A { protected string field1; }
class B : A { protected string field2; }
class C : B
{
protected string field3;
public C(A a) { field1 = a.field1; }
}
Now if I have an A and I want to "use it as a C" I can simply go C c = new C(a); and work from there.
BUT... if the reason you derived your own Tag class was because you wanted to add operations (methods) rather than data, there is another way. If you're using C# 2.0 you can write your methods as extension methods instead. For example, let's say I want to add a GoCrazy() method to A but A is defined in my generated web service proxy code and I don't want to touch that stuff (perhaps because I may need to regenerate it later). Then I can declare the extension method in any class I want, like this:
namespace MyExtentions
{
public class Whatever
{
static public void GoCrazy(this A a) { ... }
static public C ToC(this A a, string field2, string field3)
{
C c = new C(a);
c.field2 = field2;
c.field3 = field3;
}
}
}
a.GoCrazy();
C c = a.ToC("foo", "bar");
The keyword "this" before the first parameter of a static method makes the method an extension method, so the compiler can find it when "a.GoCrazy()" cannot be resolved as an instance method of A.
Hope this helps!
|
|
|
|
|
Hi,
i have a GSM modem connected to my computer, when i receive a call on my MODEM i can issue an AT command to answer the call, i use C# Serial port communication to issue the AT Commands. So after i issue AT Command to answer the call, i will play a sound file in my system using C#, I need to stream this sound into the GSM Modem, so that the person who calling can hear the sound playing on the System.
In short, i need to know how to stream audio from my computer into the GSM Modem, and also how to stream the voice from GSM modem to the System.
Hope my question is clear, Also i am not sure whether i had posted in the correct Forum topic. Thanks for your understandings.... Please do ask me any questions you have...
|
|
|
|
|
I dont know how to get voice transmission with GSM modem, I have always used it for send/receive SMS and placing calls. few months ago I had created an IVRS system, at that time I had tried using GSM modem, but no luck. so I end up using 'Internal FAX/DATA/Voice Modem' with TAPI classes, and it worked. so if you can consider that option, then that will sure work (and one more thing implementation of TAPI in .net is bit difficult).
|
|
|
|
|
Hi
I'm using DotNetOpenMail to send emails frommy .net applications, recently we updated to Exchange 2007 using active directory for authentication. this resulted in all my apps crashing, how do i add authentication to my emails,using the following code i get the error message:
Unrecognized authentication type
is there another way to to the authentication?
thanx in advance
EmailMessage emailMessage = new EmailMessage();
emailMessage.FromAddress = new EmailAddress("admin@ftech.co.za", "SMS Service");
emailMessage.AddToAddress(new EmailAddress("ismail@ftech.co.za"));
emailMessage.AddToAddress(new EmailAddress("Brynn@ftec.co.za"));
emailMessage.Subject = "Company Bulk SMS Billing " + StartDate + " - " + EndDate;
emailMessage.BodyText = @"" + StrEmialBody;
SmtpServer smtpServer = new SmtpServer("pmbex-01");
smtpServer.SmtpAuthToken = new SmtpAuthToken("administrator", "password");
emailMessage.Send(smtpServer);
|
|
|
|
|
What is DotNetOpenMail and why do you need to use more than the mail libraries built into the framework ?
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
i've been using it for a long time, i've actually never used the built in library..i once head it was not very good..
|
|
|
|
|